Tamer Essam
Tamer Essam

Reputation: 51

How to filter out json object from array in javascript

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

Upvotes: 2

Views: 8779

Answers (4)

iagowp
iagowp

Reputation: 2494

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

Upvotes: 0

ghkatende
ghkatende

Reputation: 487

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

this should give you the answer.

Upvotes: 1

Ahmad
Ahmad

Reputation: 12737

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

Upvotes: 1

munleashed
munleashed

Reputation: 1685

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

Upvotes: 1

Related Questions