[英]JS (ES6): Filter array based on nested array attributes
I have an array, which looks like this: 我有一个数组,看起来像这样:
const persons = [
{
name: "Joe",
animals: [
{species: "dog", name: "Bolt"},
{species: "cat", name: "Billy"},
]
},
{
name: "Bob",
animals: [
{species: "dog", name: "Snoopy"}
]
}
];
Now I want to filter based on the species. 现在我想根据物种进行过滤。 For example: every person which has a cat, should be returned: 例如:每个有猫的人都应该退回:
const result = [
{
name: "Joe",
animals: [
{species: "dog", name: "Bolt"},
{species: "cat", name: "Billy"},
]
}
];
I have tried with the the filter()
method like this: 我尝试过filter()
方法,如下所示:
const result = persons.filter(p => p.animals.filter(s => s.species === 'cat'))
But this doesn't return the desired result (it returns both persons). 但这不会返回所需的结果(它会返回两个人)。
How can I filter the array bases on an attribute of a nested array? 如何在嵌套数组的属性上过滤数组?
Your inner filter still returns a "truthy" value (empty array) for the dog person. 您的内部过滤器仍然为狗人返回“truthy”值(空数组)。 Add .length
so that no results becomes 0
("falsey") 添加.length
以使结果不变为0
(“falsey”)
const result = persons.filter(p => p.animals.filter(s => s.species === 'cat').length)
Edit: Per comments and several other answers, since the goal is to get a truthy value from the inner loop, .some
would get the job done even better because it directly returns true if any items match. 编辑:每个评论和其他几个答案,因为目标是从内循环获得一个真正的值, .some
会更好地完成工作,因为如果任何项匹配,它会直接返回true。
const result = persons.filter(p => p.animals.some(s => s.species === 'cat'))
你可能想用一些'
persons.filter(p => p.animals.some(s => s.species === 'cat'))
const persons = [ { name: "Joe", animals: [ {species: "dog", name: "Bolt"}, {species: "cat", name: "Billy"}, ] }, { name: "Bob", animals: [ {species: "dog", name: "Snoopy"} ] } ]; Filter = function(arr, a){ return arr.filter(t=>t.animals.filter(y=>y.species==a).length>0); } console.log(Filter(persons, 'cat'))
You can use filter()
with some()
method to check if some of the objects in animals array has species == cat
. 你可以使用filter()
和some()
方法来检查animals数组中的某些对象是否有species == cat
。
const persons = [{"name":"Joe","animals":[{"species":"dog","name":"Bolt"},{"species":"cat","name":"Billy"}]},{"name":"Bob","animals":[{"species":"dog","name":"Snoopy"}]}] const result = persons.filter(({animals}) => { return animals.some(({species}) => species == 'cat') }) console.log(result)
This should do the trick 这应该可以解决问题
persons.filter((person) => {
return person.animals.filter((animal) => {
return animal.species === 'cat';
}).length > 0;
});
Add the check on length, as filter returns an array, not a boolean. 添加长度检查,因为过滤器返回一个数组,而不是布尔值。
You havent checked against the length of the second filter. 你没有检查第二个过滤器的长度。
Filer only includes results that return a true
value. Filer仅包含返回true
值的结果。 So we need to provide the second filter with a way of returning true if it finds something. 所以我们需要为第二个过滤器提供一种如果找到某些东西则返回true的方法。 We can do that by saying, if the returned array is > 0 我们可以这样说,如果返回的数组> 0
const persons = [ { name: "Joe", animals: [ {species: "dog", name: "Bolt"}, {species: "cat", name: "Billy"}, ] }, { name: "Bob", animals: [ {species: "dog", name: "Snoopy"} ] } ]; const filtered = persons.filter(p => p.animals.filter(a => a.species === "cat").length > 0) console.log(filtered)
This should work! 这应该工作!
const result = persons.filter(person => {
let innerResult = person.animals.filter(animal => {
return animal.species === 'cat';
});
return innerResult.length > 0;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.