使用 React Reselect 过滤 2 个数组
Filtering 2 arrays with React Reselect
我正在使用 React Reselect 并且我正在尝试过滤 selectedClients,以便我只有 return clients.all 具有来自 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 的客户列表?使用上面的过滤器,我不断得到一个空数组
[]
你需要通过 client.id
而不是 all.id
const getClients = (all, selectedClientIds) => {
const selectedClients = _.filter(
all,
client => _.contains(selectedClientIds, client.id)
);
return selectedClients;
};
我正在使用 React Reselect 并且我正在尝试过滤 selectedClients,以便我只有 return clients.all 具有来自 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 的客户列表?使用上面的过滤器,我不断得到一个空数组
[]
你需要通过 client.id
而不是 all.id
const getClients = (all, selectedClientIds) => {
const selectedClients = _.filter(
all,
client => _.contains(selectedClientIds, client.id)
);
return selectedClients;
};