Muirik
Muirik

Reputation: 6289

Filtering on a Multi-Dimensional Array in Angular 2 App

I am trying to find a way to handle filtering an array with an array in my Angular 2 app. The data looks like this:

  let services = [
          {
              flags: [
                  {
                      action: "Flag One",
                      completed: true,

                  },
                  {
                      action: "Flag Two",
                      completed: false,

                  },
                ],
            ribbons: [
                 {
                      action: 'Ribbon One',
                      active: false,
                  },
                 {
                      action: 'Ribbon Two',
                      active: true,
                  },
            ]
          }
        ]

Now, if I know what item within the second array to target, I can do this:

let filteredServices = services.filter(service => service.flags[0].completed === false);

console.dir(filteredServices);

However, generally I don't know which item within the inner array to target, so how can I write a filter function to iterate over both arrays and filter for the specific item I'm looking for? Would I use a combination of "filter" and "forEach"? Or is there a more succinct way to do this?

Upvotes: 3

Views: 2382

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222720

You can use filter with some

let filteredServices = services.filter((element) => element.flags.some((subElement) => subElement.completed === false));

Upvotes: 1

Related Questions