【问题标题】:Get value based on sub-array unique value根据子数组唯一值获取值
【发布时间】:2021-04-09 01:14:16
【问题描述】:

我的JSON 数据如下所示:

{
    "data": [{
            "id": "1",
            "blogTitle": "How to plant a tree?",
            "categories": [{
                "CategoryID": "10",
                "CategoryTitle": "Apple Tree"
            }, {
                "CategoryID": "11",
                "CategoryTitle": "Mango Tree"
            }, {
                "CategoryID": "42",
                "CategoryTitle": "Banana Tree"
            }]
        },
        {
            "id": "2",
            "blogTitle": "How to make Juice?",
            "categories": [{
                "CategoryID": "71",
                "CategoryTitle": "Apple Juice"
            }, {
                "CategoryID": "72",
                "CategoryTitle": "Mango Juice"
            }, {
                "CategoryID": "73",
                "CategoryTitle": "Banana Juice"
            }]

        }
    ]
}

我想要的是通过传递CategoryID的值来获得id的值

例如:如果我发送10,那么我应该得到1 作为回报,因为"CategoryID": "10""id": "1" 在同一块中

每个CategoryID 都是唯一的,从而导致id 的唯一值

我做了什么?

  • 我想出的最接近的方法是使用array.filter(),但我能够过滤id 值,给出id 的值,但是我如何获得id 的值,给定CategoryID

有人可以帮帮我吗?

【问题讨论】:

    标签: javascript arrays reactjs object


    【解决方案1】:

    你可以整合你的数据,然后像这样使用Array#some

    const yourObject = {"data":[{"id":"1","blogTitle":"How to plant a tree?","categories":[{"CategoryID":"10","CategoryTitle":"Apple Tree"},{"CategoryID":"11","CategoryTitle":"Mango Tree"},{"CategoryID":"42","CategoryTitle":"Banana Tree"}]},{"id":"2","blogTitle":"How to make Juice?","categories":[{"CategoryID":"71","CategoryTitle":"Apple Juice"},{"CategoryID":"72","CategoryTitle":"Mango Juice"},{"CategoryID":"73","CategoryTitle":"Banana Juice"}]}]};
    
    const find_ID_BasedOn_GivenCategoryId = (categoryId) => {
      for(const item of yourObject.data){
        if(item.categories && item.categories.some(r => r.CategoryID == categoryId))
          return item.id;
      }
      
      return "Not found";
    }
    
    console.log(find_ID_BasedOn_GivenCategoryId(11));
    console.log(find_ID_BasedOn_GivenCategoryId(71));
    console.log(find_ID_BasedOn_GivenCategoryId(999));

    更简单的方法: 使用Array#find

    const yourObject = {"data":[{"id":"1","blogTitle":"How to plant a tree?","categories":[{"CategoryID":"10","CategoryTitle":"Apple Tree"},{"CategoryID":"11","CategoryTitle":"Mango Tree"},{"CategoryID":"42","CategoryTitle":"Banana Tree"}]},{"id":"2","blogTitle":"How to make Juice?","categories":[{"CategoryID":"71","CategoryTitle":"Apple Juice"},{"CategoryID":"72","CategoryTitle":"Mango Juice"},{"CategoryID":"73","CategoryTitle":"Banana Juice"}]}]};
    
    const find_ID_BasedOn_GivenCategoryId = (categoryId) => {
      const result = yourObject.data.find(item => 
                    item.categories && item.categories.some(r => r.CategoryID == categoryId));
      
     return result ? result.id : "Not found"; 
    }
    
    console.log(find_ID_BasedOn_GivenCategoryId(11));
    console.log(find_ID_BasedOn_GivenCategoryId(71));
    console.log(find_ID_BasedOn_GivenCategoryId(999));

    【讨论】:

    • 感谢您撰写答案。我收到此错误:Cannot read property 'some' of undefined
    • 更新了我的答案,请看一下@floss
    • 谢谢!有用。如果categoryId 不存在怎么办?
    • 您可以像更新后的答案一样返回Not found
    【解决方案2】:

    您可以使用findsome 的组合来检查您想要的categoryID,例如:

    const data = {
        "data": [{
                "id": "1",
                "blogTitle": "How to plant a tree?",
                "categories": [{
                    "CategoryID": "10",
                    "CategoryTitle": "Apple Tree"
                }, {
                    "CategoryID": "11",
                    "CategoryTitle": "Mango Tree"
                }, {
                    "CategoryID": "42",
                    "CategoryTitle": "Banana Tree"
                }]
            },
            {
                "id": "2",
                "blogTitle": "How to make Juice?",
                "categories": [{
                    "CategoryID": "71",
                    "CategoryTitle": "Apple Juice"
                }, {
                    "CategoryID": "72",
                    "CategoryTitle": "Mango Juice"
                }, {
                    "CategoryID": "73",
                    "CategoryTitle": "Banana Juice"
                }]
    
            }
        ]
    };
    
    
    const findId = (categoryID, { data }) => {
        const foundItem = data.find(({ categories }) => 
          categories.some(({ CategoryID }) => CategoryID == categoryID)
        );
        return foundItem ? foundItem.id : null;
    }
    
    console.log(findId(71, data))

    【讨论】:

    • categoriesundefinenull 时,您将收到类似Cannot read property 'some' of undefined 的错误。更多详情请查看上面的my answer :)
    【解决方案3】:

    找到+一些

    var data = [
      {
        id: "1",
        blogTitle: "How to plant a tree?",
        categories: [
          {
            CategoryID: "10",
            CategoryTitle: "Apple Tree"
          },
          {
            CategoryID: "11",
            CategoryTitle: "Mango Tree"
          },
          {
            CategoryID: "42",
            CategoryTitle: "Banana Tree"
          }
        ]
      },
      {
        id: "2",
        blogTitle: "How to make Juice?",
        categories: [
          {
            CategoryID: "71",
            CategoryTitle: "Apple Juice"
          },
          {
            CategoryID: "72",
            CategoryTitle: "Mango Juice"
          },
          {
            CategoryID: "73",
            CategoryTitle: "Banana Juice"
          }
        ]
      }
    ];
    
    const findId = (id) => {
      return data?.find((item) =>
        item?.categories?.some((i) => i?.CategoryID === id)
      )?.id;
    };
    
    
    console.log(findId("10")); //1
    

    【讨论】:

    • 找不到时会收到类似.id; 的错误。
    • 修复错误:添加可选链接。不喜欢时它会返回undefined
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多