0

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

enter image description here

4
  • Why are you using ===? There is no reason whatsoever for you to do use === in the type of code you're writing. Commented Jan 10, 2023 at 15:48
  • Does this answer your question? Function with forEach returns undefined even with return statement Commented Jan 10, 2023 at 15:48
  • You're accessing item.naam but the screenshot shows the property is named Intresse_naam. You'll probably want to use a regex as well since that data seems to be somewhat sketchy, e.g., at least one value includes escaped characters. Commented Jan 10, 2023 at 15:49
  • 3
    forEach does nothing w/ return values; you need to return something from filter. Commented Jan 10, 2023 at 15:53

1 Answer 1

1

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'); 
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.