用于通过嵌套数组ngrx循环的选择器

发布于 2025-02-09 15:06:46 字数 1235 浏览 2 评论 0原文

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

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

以下是我尝试获得列表的嵌套服务,我需要帮助获取项目

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

In order to access the items array from this JSON, how should I use a selector?

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

The following is what I tried to get nestedServices for the list, I needed help in fetching items

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清泪尽 2025-02-16 15:06:46

List是一个对象,因此使用MAP是一个错误。使用选择器以获取从商店确切需要的东西是正确的选择。

只需返回您想要的确切属性:

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

list is an object, so using map is a mistake. Using a selector for grabbing what you exactly need from the store is the right choice.

Just return the exact property you want:

// this will return the wanted array
export const getItems = createSelector(
   selectState,
   (state) =>  state.list['nestedService']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文