Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write filter that inspects an array of arrays

Consider the following arrays:

 array1 = ["A","B", "C"];
 array2 = ["D", "E", "F"];
 array3 = ["G", "H", "I"];

 combined = [array1, array2, array3];
 select = ["A","H"];

I need a filter for 'combined' based on the 'select' array that returns array1 and array3 as A is in Array1, and H is in array3.

This is what I have tried (in typescript)

return routes.filter((route: any) =>  
             roles.some((role: string) =>
                 route.config.roles.some((routeRole: string) =>
                     routeRole === role)));

It seems to work, but it only works for the first item in the route.config.roles.

like image 728
Greg Gum Avatar asked Dec 04 '25 18:12

Greg Gum


1 Answers

Use filter and some:

function finder(combined, select) {
  return combined.filter(function (el) {
    return el.some(function (letter) {
      return select.indexOf(letter) > -1;
    });
  });
}

DEMO

like image 98
Andy Avatar answered Dec 06 '25 12:12

Andy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!