0

I'm using React Reselect and I'm trying to filter the selectedClients such that I only return clients.all that have id's from selectedClientsIds.

selected_clients.js

import _ from 'lodash';
import { createSelector } from 'reselect';

const clientSelector = state => state.clients.all
const selectedClientSelector = state => state.selectedClientIds

const getClients = (all, selectedClientIds) => {
    const selectedClients = _.filter(
        all,
        client => _.contains(selectedClientIds, all.id)
    );

   return selectedClients;
};

export default createSelector(
    clientSelector,
    selectedClientSelector,
    getClients
);

The clients.all store looks like this:

[
    {
        "id": 1,
        "name": “Bob”,
    },
    {
        "id": 2,
        "name": “Mary ”,
    }
]

The selectedClientIds store looks like this:

[1]

How would I combine both to only show a list of clients that have id's from selectedClientId's? With the filter I have above, I keep getting an empty array

 []
lost9123193
  • 10,460
  • 26
  • 73
  • 113

1 Answers1

2

You need to pass client.id not all.id

const getClients = (all, selectedClientIds) => {
    const selectedClients = _.filter(
        all,
        client => _.contains(selectedClientIds, client.id)
    );

   return selectedClients;
};
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98