[英]Filter an array within an array of objects
I have an array of 10 users, I want to filter the users based on the 'intresses' (that means interests in dutch) array within the users.我有一个包含 10 个用户的数组,我想根据用户中的“intresses”(即对荷兰语的兴趣)数组来过滤用户。 So for example I want to filter all the users that have the interests 'Afrika' (like index 2).
因此,例如,我想过滤所有对“非洲”感兴趣的用户(如索引 2)。
This is what I tried but it will give me an empty array back.这是我尝试过的,但它会返回一个空数组。
var newArray = gesorteerdeMatchPercentages.filter((el) => {
el.intresses.forEach((item) => {
return item.naam === "Afrika";
});
});
console.log("new", newArray);
If i understand you correctly you could do that using Array.prototype.some()
like this如果我理解正确的话,你可以像这样使用
Array.prototype.some()
来做到这一点
var newArray = gesorteerdeMatchPercentages.filter((el) => {
return el.intresses.some(el => el.naam === 'Afrika');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.