lost9123193 lost9123193 - 4 months ago 32
React JSX Question

Filtering 2 arrays with React Reselect

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

[]

Answer

You need to pass client.id not all.id

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

   return selectedClients;
};
Comments