Filtering in a multidimensional array in Angular 2 application

I am trying to find a way to handle filtering an array with an array in my Angular 2 application. 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 which element in the second array should be targeted, I can do this:

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

console.dir(filteredServices);

      

However, generally I don't know which element within the inner array should be targeted, so how can I write a filter function to iterate over the arrays and filter for the particular element I'm looking for? Am I using a combination of "filter" and "forEach"? Or is there a more concise way to do this?

+3


source to share


1 answer


You can use the filter with some



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

      

+1


source







All Articles