2

How do I apply filter within a filter in JavaScript while looking for values in an array within the array?

Say, I want to get all construction projects whose teams include Jane Smith.

I realize I'm inventing some crazy JavaScript here, I'm just trying to show you what I'm after.

const result = myArray.filter(x => x.type === "construction" && x.team.filter(y => y.name.includes("Jane Smith")));

Here's the array I want to filter:

[
   {
      "type": "construction",
      "name": "Project A",
      "team": [
         {
            "name": "John Doe",
            "position": "Engineer"
         },
         {
            "name": "Jane Smith",
            "position": "Architect"
         }
      ]
   },
   {
      "type": "construction",
      "name": "Project B",
      "team": [
         {
            "name": "Walt Disney",
            "position": "Creative Director"
         },
         {
            "name": "Albert Einstein",
            "position": "Scientist"
         }
      ]
   },
   {
      "type": "remodelling",
      "name": "Project C",
      "team": [
         {
            "name": "John Travolta",
            "position": "Manager"
         }
      ]
   }
]

2 Answers 2

4

You could use Array#some for checking the team.

This proposal uses destructuring assignment for the objects.

var array = [{ type: "construction", name: "Project A", team: [{ name: "John Doe", position: "Engineer" }, { name: "Jane Smith", position: "Architect" }] }, { type: "construction", name: "Project B", team: [{ name: "Walt Disney", position: "Creative Director" }, { name: "Albert Einstein", position: "Scientist" }] }, { type: "remodelling", name: "Project C", team: [{ name: "John Travolta", position: "Manager" }] }],
    result = array.filter(({ type, team }) =>
        type === "construction" && team.some(({ name }) => name === "Jane Smith"));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

3 Comments

I think it should be name === "Jane Smith" rather than name.includes("Jane Smith"). Unless we want to match against, for example, "Jane Smithers".
This seems very nice and clean -- with @thesilkworm's suggestion. I'm testing it. Once I make sure it's working, I'll accept yours and the answer. Thank you!
Works beautifully and the structure is easy to understand. Thank you!
1

Maybe something like the following?

const result = myArray.filter(x => x.type === "construction" && x.team.filter(y => y.name.includes("Jane Smith")).length > 0);

Basically I'm just checking to see if that new filtered array length is greater than 0.

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.