skip to Main Content

Seems like this should be very simple; I have a 2D array:

[["Apple",123],
 ["Berry",234],
 ["Cherry",345],
 ["Dog",456]]

I have a list:

["Apple","Dog"]

I would like to return an array based on the list items:

[["Apple",123],
 ["Dog",456]]

Note: the real 2D array has 100 rows and 10 columns. Thanks !

Bonus, a command that will filter to just "Apple" and "Dog" rows as well as "Product" and "Quality" columns:

[["Product","Quantity","Quality"],
 ["Apple",123,"Red"],
 ["Berry",234,"Blue"],
 ["Cherry",345,"Choke"],
 ["Dog",456,"Big"]]

Expected result:

[["Product","Quality"],
 ["Apple","Red"],
 ["Dog","Big"]]

or

[["Apple","Red"],
 ["Dog","Big"]]

Again, the order and number of columns and rows is variable.

Update:

Based on the two answers below, neither filtering approach seems to work for my use-case. As such, I will provide the following context. Rather than "Products" like "Apple" and "Dog", my array is more like:

[["URL","Quantity","Quality"],
 ["http://abcd.defg.eghi.com/alphabet/abcd1234-ef12-23ed-eafe/q?key=123-abc",123,"Red"],
 ["http://abcd.defg.eghi.com/alphabet/aecd934-ef12-23ed-jufe/q?key=176-abc",234,"Blue"],
 ["http://abcd.defg.eghi.com/alphabet/eafcd134-ef24-23ed-eefe/q?key=145-abc",345,"Choke"],
 ["http://abcd.deeg.eghi.com/alphabet/abcd1234-ef12-23ed-45fe/q?key=187-abc",456,"Big"]]

and my list is like:

["http://abcd.defg.eghi.com/alphabet/abcd1234-ef12-23ed-eafe/q?key=123-abc","http://abcd.deeg.eghi.com/alphabet/abcd1234-ef12-23ed-45fe/q?key=187-abc"]

My expectation is that the first and fourth row would be returned, as with Apple and Dog, but for some reason the URLs aren’t filtering. Are there any special characters that may be affecting this function ?

2

Answers


  1. function tooeasy() {
      let arr2 = ["Apple","Dog"];
      let arr1 = [["Apple",123],["Berry",234],["Cherry",345], ["Dog",456]].filter(r => ~arr2.indexOf(r[0]));
      Logger.log(arr1);
    }
    
    Execution log
    7:46:28 PM  Notice  Execution started
    7:46:28 PM  Info    [[Apple, 123.0], [Dog, 456.0]]
    7:46:30 PM  Notice  Execution completed
    

    Only one question at at time

    Login or Signup to reply.
  2. Based on yout second use case. You can use various of Array functions to achieve it. Here’s an example:

    const data = [
      ["URL", "Quantity", "Quality"],
      ["http://abcd.defg.eghi.com/alphabet/abcd1234-ef12-23ed-eafe/q?key=123-abc", 123, "Red"],
      ["http://abcd.defg.eghi.com/alphabet/aecd934-ef12-23ed-jufe/q?key=176-abc", 234, "Blue"],
      ["http://abcd.defg.eghi.com/alphabet/eafcd134-ef24-23ed-eefe/q?key=145-abc", 345, "Choke"],
      ["http://abcd.deeg.eghi.com/alphabet/abcd1234-ef12-23ed-45fe/q?key=187-abc", 456, "Big"]
    ];
    
    function filterData(data, columns, targets) {
      const [headers, ...rows] = data;
      const filterIndex = headers.indexOf(columns[0]);
      const indices = columns.map(column => headers.indexOf(column));
      return [columns,
        ...rows.filter(row => targets.includes(row[filterIndex]))
        .map(row => row.filter((item, i) => indices.includes(i)))
      ];
    }
    
    // columns you want to keep
    const columns = ["URL", "Quality"];
    
    // targets you want to filter
    const urls = [
      "http://abcd.defg.eghi.com/alphabet/abcd1234-ef12-23ed-eafe/q?key=123-abc",
      "http://abcd.deeg.eghi.com/alphabet/abcd1234-ef12-23ed-45fe/q?key=187-abc"
    ];
    
    const filtered = filterData(data, columns, urls);
    console.log(filtered);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search