Home > Net >  How to keep it a 2D array using filter()?
How to keep it a 2D array using filter()?

Time:06-22

May seem silly, but I didn't find anything that wouldn't resort to another function, so...

ar1 = [[1, 10, "Item1", 50], [1, 10, "Item1", 50],[1, 10, "Item3", 50]];
let ar2 = ar1.filter(e => e[2] === "Item1")[0];//To get only of the filtered records

It returns:

[1, 10, "Item1", 50]

but to get it to return:

[[1, 10, "Item1", 50]]

Thank you!

CodePudding user response:

Perhaps this:

function lfunko() {
  const a = [[1, 10, "Item1", 50], [1, 10, "Item1", 50],[1, 10, "Item3", 50]];
  Logger.log(a.filter(r => r[2] == "Item1").filter((r,i) => i == 1));
}

Execution log
12:42:24 PM Notice  Execution started
12:42:23 PM Info    [[1.0, 10.0, Item1, 50.0]]
12:42:25 PM Notice  Execution completed
  • Related