简体   繁体   中英

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

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM