简体   繁体   English

如何从 javascript 中的数组中过滤掉 json object

[英]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我有这个 JSON 响应我想过滤掉包含 arrays 类别中特定 id 的产品

[
 {
    "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 112 的 object假设我想要上面给出的 JSON object 的结果

[
 {
    "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在 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这就是我尝试过的,它正在过滤掉取决于对象内的字符串,但我找不到过滤掉取决于类别 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);

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM