filter
method runs a given function
on every item
in the array
and returns an array of all items for which the function returns true
. This will help us to get each sub-array of the mainArray
.
let mainArray = [
['a', 'b'],
['c', 'd'],
['e', 'f']
];
// Extract sub-arrays with filter
let temp = mainArray.filter(function(item, index, array){
return array;
});
console.log({temp});
Now, we need to compare every sub-array with the subArray
. For this we will use the every
method. This method always return a boolean value true
or false
. That is base on the given function
on every item in the array and returns true
. If that function
returns true for every true; what's that mean? Well, this mean every
will return false
, unless every item return true
.
let searchArray = ['c', 'd'];
// Extract every letter from the sub-array
temp = searchArray.every(function(item, index, array){
console.log({item});
return true;
});
console.log({temp});
Finally, includes
method determines whether an array includes a certain value among its entries, returning true
or false
as appropriate.`
let searchArray = ['c', 'd'];
// Compare a letter in the searchArray.
let tempItem = 'c';
temp = searchArray.includes(tempItem);
console.log({temp});
Now, we can combine all and replace functions
with the Arrow Function Syntax. And happy coding.
let mainArray = [
['a', 'b'],
['c', 'd'],
['e', 'f']
];
let searchArray = ['c', 'd'];
// All together
let results = mainArray.filter(array => array.every(item => searchArray.includes(item)));
console.log({results});
roles.filter(role => role.groups.find(group => user.groups.includes(group.id)))
\$\endgroup\$const roles
and then you try to redefine it. \$\endgroup\$