Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Thank you!

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading