选择器用于遍历嵌套数组ngrx



为了从这个JSON访问项目数组,我应该如何使用选择器?

const list  = {
"nestedService": [
{
"categories": [
{
"name": "test1",
"items": [
{
"itemCat": [
{
"code": "123"
}
]
}
]
},
{
"name": "test2",
"items": [
{
"itemCat": [
{
"code": "345"
}
]
}
]
}
]
}
]
}

下面是我尝试获取列表的nestedServices,我需要帮助来获取项目

export const getItems =
createSelector(selectState,(state) =>  {
const data = state.list
return data?.map(e => e.nestedService);
});

list是一个对象,所以使用map是一个错误。使用selector从商店中抓取你确切需要的东西是正确的选择。

返回你想要的属性:

// this will return the wanted array
export const getItems = createSelector(
selectState,
(state) =>  state.list['nestedService']);

最新更新