简体   繁体   English

使用嵌套数组过滤对象数组

[英]Filter Array of objects with nested array

so I am trying to set up a nested filter on an array of objects.所以我试图在对象数组上设置嵌套过滤器。 The thing is that the filter is applied inside the object on a key that is another array of objects.问题是过滤器在 object 内部应用在另一个对象数组的键上。

here is the code:这是代码:

const items = [
  { name: "123", id: 1, value:  true, arr: [{ id: 1 }] },
  { name: "456", id: 2, value: false, arr: [{ id: 2 }] },
  { name: "456", id: 2, value: false, arr: [{ id: 3 }] },
  { name: "456", id: 2, value: false, arr: [{ id: 4 }] },
  { name: "456", id: 2, value: false, arr: [{ id: 5 }] },
  { name: "456", id: 2, value: false, arr: [{ id: 6 }] },
];

const newArray = items.filter((objects) => {
  objects.arr.filter((item) => {
    if (item.id === 2) {
      return objects;
    }
     
  });
});
console.log(newArray);

I 'm not sure where to put the return because in this situation i just get an empty array.我不确定将返回值放在哪里,因为在这种情况下我只是得到一个空数组。

You need to check the nested array contains the wanted id and return the result to the filter method.您需要检查嵌套数组是否包含所需的id ,并将结果返回给 filter 方法。

 const items = [{ name: "123", id: 1, value: true, arr: [{ id: 1 }] }, { name: "456", id: 2, value: false, arr: [{ id: 2 }] }, { name: "456", id: 2, value: false, arr: [{ id: 3 }] }, { name: "456", id: 2, value: false, arr: [{ id: 4 }] }, { name: "456", id: 2, value: false, arr: [{ id: 5 }] }, { name: "456", id: 2, value: false, arr: [{ id: 6 }] }], result = items.filter(({ arr }) => arr.some(({ id }) => id === 2)); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

Use Array#some to check if current arr has an element with id equal to 2:使用Array#some检查当前arr是否有一个id等于 2 的元素:

 const items = [ { name: "123", id: 1, value: true, arr: [{ id: 1 }] }, { name: "456", id: 2, value: false, arr: [{ id: 2 }] }, { name: "456", id: 2, value: false, arr: [{ id: 3 }] }, { name: "456", id: 2, value: false, arr: [{ id: 4 }] }, { name: "456", id: 2, value: false, arr: [{ id: 5 }] }, { name: "456", id: 2, value: false, arr: [{ id: 6 }] } ]; const newArray = items.filter(({ arr = [] }) => arr.some(({ id }) => id === 2) ); console.log(newArray);

As you contains only one object in arr then you can access this object by using [0] index.由于您在arr中仅包含一个object ,因此您可以使用[0]索引访问此 object。

Working Demo:工作演示:

 const items = [ { name: "123", id: 1, value: true, arr: [{ id: 1 }] }, { name: "456", id: 2, value: false, arr: [{ id: 2 }] }, { name: "456", id: 2, value: false, arr: [{ id: 3 }] }, { name: "456", id: 2, value: false, arr: [{ id: 4 }] }, { name: "456", id: 2, value: false, arr: [{ id: 5 }] }, { name: "456", id: 2, value: false, arr: [{ id: 6 }] }, ]; const newArray = items.filter((obj) => { if (obj.arr[0].id === 2) { return obj; } }); console.log(newArray);

I answered you as per the issue you are facing but if you have multiple objects in your arr then you can go ahead with Array.some() method as suggested by other answers.我根据您面临的问题回答了您,但是如果您的arr中有多个对象,那么您可以按照其他答案的建议使用Array.some()方法提前 go。

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

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