2

I have this JSON response I want to filter out the product that contains specific id inside the categories arrays

[
 {
    "id": 7721,
    "name": "Grilled kebab corn tomato special",
    "purchase_note": "",
    "categories": [
        {
            "id": 50,
            "name": "All recipes",
            "slug": "0-all-recipes"
        },
        {
            "id": 94,
            "name": "Chef's Special",
            "slug": "chefs-special"
        }
    ]
 },
 {
    "id": 7722,
    "name": "Grilled kebab corn tomato special",
    "purchase_note": "",
    "categories": [
        {
            "id": 112,
            "name": "All recipes",
            "slug": "0-all-recipes"
        },
        {
            "id": 22,
            "name": "Chef's Special",
            "slug": "chefs-special"
        }
    ]
 }
]

Here's an example I want to filter out the object that contains id 112 in the categories array let's say I want this result from the JSON object given above

[
 {
    "id": 7722,
    "name": "Grilled kebab corn tomato special",
    "purchase_note": "",
    "categories": [
        {
            "id": 112,
            "name": "All recipes",
            "slug": "0-all-recipes"
        },
        {
            "id": 22,
            "name": "Chef's Special",
            "slug": "chefs-special"
        }
    ]
  }
]

In JavaScript

Thanks (:

That's what I have tried and it's working to filter out depends on the string inside the objects but I couldn't find a way to filter out depends on the categories id

here's my try

  menuData = the json response 

 menuData.filter(menuData => menuData.name == "Grilled kebab corn tomato special");

also, I've tried to use this

 menuData = the json response 

 menuData.filter(menuData => menuData.categories.map((o) => o.id) == 112);

2
  • 2
    If you have tried to filter it please show what you've tried and we can help you get past problems Commented Apr 22, 2021 at 21:59
  • I've updated the question Commented Apr 22, 2021 at 22:06

4 Answers 4

1

You can simply do this:

const filtered = items.filter(item =>
  item.categories.filter(c => c.id == 112).length > 0

);

const items = [{
    "id": 7721,
    "name": "Grilled kebab corn tomato special",
    "purchase_note": "",
    "categories": [{
        "id": 50,
        "name": "All recipes",
        "slug": "0-all-recipes"
      },
      {
        "id": 94,
        "name": "Chef's Special",
        "slug": "chefs-special"
      }
    ]
  },
  {
    "id": 7722,
    "name": "Grilled kebab corn tomato special",
    "purchase_note": "",
    "categories": [{
        "id": 112,
        "name": "All recipes",
        "slug": "0-all-recipes"
      },
      {
        "id": 22,
        "name": "Chef's Special",
        "slug": "chefs-special"
      }
    ]
  }
];


const filtered = items.filter(item =>
  item.categories.filter(c => c.id == 112).length > 0

);

console.log(filtered);

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

2 Comments

Thank you Ahmed really appreciate ur help
filter is not the best method for the inner function, as it will go through all the array without needing to some times
1

const filtered = data.filter((product) => {
    return product.categories.filter(cat => cat.id === 112)
})

Comments

1
console.log(your_array.filter(val =>  val.categories.find( item => item.id===112 ))  

this should give you the answer.

Comments

0

So what you want to do is filter out some elements if their categories have some category matching some parameters

so you have

menuData.filter(menu => {
  return !menu.categories.some(category => category.id === 112)
});

Comments

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.