简体   繁体   中英

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. So for example I want to filter all the users that have the interests 'Afrika' (like index 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

var newArray = gesorteerdeMatchPercentages.filter((el) => {
 return el.intresses.some(el => el.naam === 'Afrika'); 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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