Home > database >  Skript not working arr.filter() in google apps
Skript not working arr.filter() in google apps

Time:07-26

This is my script

    var arr1 = [[1],[2],[3],[4]],
        arr2 = [[2],[4]],
        res = arr1.filter( item => arr2.includes( item[0] ) );
    Logger.log(res);

And it doesn't work. What's wrong here?

CodePudding user response:

Instead of using includes, you can use find, because you have arrays inside arrays.

var arr1 = [[1], [2], [3], [4]],
  arr2 = [[2], [4]],
  res = arr1.filter((item) => arr2.find((el) => el[0] === item[0]));
console.log(res);

CodePudding user response:

If you sure that the all sub arrays of arr2 contain just one element always, you can fix your code if you just add .flat() method in the second line:

var arr1 = [[1],[2],[3],[4]],
    arr2 = [[2],[4]].flat(), // <-------------------------- here
    res = arr1.filter(item => arr2.includes(item[0]));

console.log(res);

  • Related