我有订单减速器,它有很多状态。
const initialState = {
channel: null,
order: {},
fetching:true,
menu: [],
categories: [],
subcategories: [],
currentCategoryId: 1,
currentSubcategoryId: 5,
currentMenu: [],
};
我要过滤的是 menu
。 menu
是状态数组,它的对象为 menu_item
我有 currentCategoryId
和 currentSubcategoryId
What I want to do with these states is that by using currentCategoryId
and currentSubcategoryId
to filter menu
and put filtered states to currentMenu
.
case Constants.ORDER_CHANNEL_CONNECTED:
return
{...state,currentMenu: action.menu.map((menu) => {
if(state.currentCategoryId == menu.category_id){
return menu;
}
else return false;}}
为此,我编写了上面的代码。即使它返回一些过滤值,它也会显示相同数量的具有许多错误值的数组。我想找到其他方法来做到这一点..
我怎样才能做到这一点?
提前致谢。
原文由 D.R 发布,翻译遵循 CC BY-SA 4.0 许可协议
请使用
filter
函数:PS:我同意下面的答案,最好使用 Immutable.js