最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to filter out json object from array in javascript - Stack Overflow

programmeradmin5浏览0评论

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

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

Share Improve this question edited Apr 22, 2021 at 22:05 Tamer Essam asked Apr 22, 2021 at 21:56 Tamer EssamTamer Essam 611 silver badge6 bronze badges 2
  • 2 If you have tried to filter it please show what you've tried and we can help you get past problems – Always Learning Commented Apr 22, 2021 at 21:59
  • I've updated the question – Tamer Essam Commented Apr 22, 2021 at 22:06
Add a ment  | 

4 Answers 4

Reset to default 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);

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

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

this should give you the answer.

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)
});
发布评论

评论列表(0)

  1. 暂无评论