简体   繁体   English

如何使用 filter() 将其保留为 2D 数组?

[英]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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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