How to compare two 2d Arrays and return the result in a specific order (Google Apps Script)?

Viewed 82

I have a 2d array that I want to remove values that are not in a second 2d array and have it sorted in the same order as the second 2d array.

//Here is the first 2D Array.

  [
    [311112, 1, 2, 4, 5, '', '', '', '', '', '', 2, 3],
    [323233, '', '', '', '', '', 2, 4, 4, '', '', '', ''],
    [321567, 1, 2, 4, 5, '', '', '', '', '', '', 2, 3],
    [312222, 4, 1, 6, 7, '', 3, '', 3, '', '', 5, 3],
    [326565, '', '', '', '', '', '', 4, 3, 1, 5, '', ''],
    [322355, '', '', '', '', '', '', '', '', 1, 4, '', ''],
    [312855, '', '', '', '', '', 2, 3, '', '', '', '', ''],
    [329887, '', 2, 3, 6, '', '', '', '', '', '', '', ''],
    [326776, '', '', '', '', '', 2, 3, '', '', '', '', ''],
    [325556, '', 5, 3, '', 1, '', '', '', '', '', '', '']
  ];

//This second 2d array is a list of ids that get generated by another list.  

[
    [326565],
    [321567],
    [326776],
    [312855],
    [329887],
    [333565],
    [311112],
    [359909]
  ];

//Expected result arrangement.

  [
    [326565, '', '', '', '', '', '', 4, 3, 1, 5, '', ''],
    [321567, 1, 2, 4, 5, '', '', '', '', '', '', 2, 3],
    [326776, '', '', '', '', '', 2, 3, '', '', '', '', ''],
    [312855, '', '', '', '', '', '', '', '', '', '', '', ''],
    [329887, '', 2, 3, 6, '', '', '', '', '', '', '', ''],
    [333565, '', '', '', '', '', '', '', '', '', '', '', ''],
    [311112, 1, 2, 4, 5, '', '', '', '', '', '', 2, 3],
    [359909, '', '', '', '', '', '', '', '', '', '', '', '']
  ];

//Desired end result is to have the id column removed.

  [
    ['', '', '', '', '', '', 4, 3, 1, 5, '', ''],
    [1, 2, 4, 5, '', '', '', '', '', '', 2, 3],
    ['', '', '', '', '', 2, 3, '', '', '', '', ''],
    ['', '', '', '', '', '', '', '', '', '', '', ''],
    ['', 2, 3, 6, '', '', '', '', '', '', '', ''],
    ['', '', '', '', '', '', '', '', '', '', '', ''],
    [1, 2, 4, 5, '', '', '', '', '', '', 2, 3],
    ['', '', '', '', '', '', '', '', '', '', '', '']
  ];

In the example you will notice that the first 2d array contains some Ids that the second does not and vise versa. Would it be better to change the script that builds the first 2d array and have it return in the order of the second or is there a low cost method to rearrange the data as described in the example?

1 Answers

You could take a Map and map the arrays without the first element.

const
    data = [[311112, 1, 2, 4, 5, '', '', '', '', '', '', 2, 3], [323233, '', '', '', '', '', 2, 4, 4, '', '', '', ''], [321567, 1, 2, 4, 5, '', '', '', '', '', '', 2, 3], [312222, 4, 1, 6, 7, '', 3, '', 3, '', '', 5, 3], [326565, '', '', '', '', '', '', 4, 3, 1, 5, '', ''], [322355, '', '', '', '', '', '', '', '', 1, 4, '', ''], [312855, '', '', '', '', '', 2, 3, '', '', '', '', ''], [329887, '', 2, 3, 6, '', '', '', '', '', '', '', ''], [326776, '', '', '', '', '', 2, 3, '', '', '', '', ''], [325556, '', 5, 3, '', 1, '', '', '', '', '', '', '']],
    ids = [[326565], [321567], [326776], [312855], [329887], [333565], [311112], [359909]],
    map = new Map(data.map(([k, ...v]) => [k, v])),
    result = ids
        .map(([k]) => map.get(k) || ['', '', '', '', '', '', '', '', '', '', '', '']);

console.log(result);

Related