1

My JSON data looks like this:

{
    "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"
            }]

        }
    ]
}

What I want is to get the value of id by passing the value of CategoryID

For example: if I send 10 then I should get 1 in return because "CategoryID": "10" is in the same block with "id": "1"

Each CategoryID is unique resulting in a unique value of id

What did I do?

  • Closest I came up with was using the array.filter() but I am able to filter id value give the value of id but how do I get the value of id, given CategoryID

Could someone please kindly help me?

3 Answers 3

4

You can integrate your data, then use Array#some like this.

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));

Simpler approach: using 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));

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for composing the answer. I am getting this error: Cannot read property 'some' of undefined
Updated my answer, pls take a look at it @floss
thank you! it works. what if the categoryId does not exists?
You can return Not found like the updated answer.
1

You can use find with a combination of some for checking the categoryID that you want, for example:

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))

1 Comment

You will get an error like Cannot read property 'some' of undefined when categories is undefine or null. Check my answer above in more details :)
1

find + some

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

2 Comments

You will get an error at this like .id; when not found.
fix error: add optional chaining. It will return undefined, when not fond.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.