使用React Reselect过滤2个数组

时间:2016-08-03 17:49:47

标签: reactjs redux lodash reselect

我正在使用React Reselec t而我正在尝试过滤selectedClients,以便我只返回来自selectedClientsIds的id的所有客户端。

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
);

clients.all商店看起来像这样:

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

selectedClientIds商店如下所示:

[1]

我如何将两者结合起来只显示来自selectedClientId的id的客户列表?使用上面的过滤器,我不断得到一个空数组

 []

1 个答案:

答案 0 :(得分:2)

您需要传递client.id而不是all.id

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

   return selectedClients;
};