Search code examples
arraysgoogle-apps-scriptgoogle-sheetscomparison

How to compare arrays and remove duplicates in GAS?


How to get this one to yield a list of the unique ID's after comparison?

enter image description here

It's giving me an empty list, although everything seems to be fine.


Solution

  • Try with filter and includes.

    If you want to check for values that are in uniqueIDs1 but not in uniqueIDs2:

    let uniqueIDs1 = [1,2,3,4,5,6];
    let uniqueIDs2 = [1,3,4,6];
    let unique_values = uniqueIDs1.filter(x => !uniqueIDs2.includes(x));
    console.log(unique_values) // [ 2, 5 ]

    If you want to check for values that are in uniqueIDs1 but not in uniqueIDs2 and in uniqueIDs2 but not in uniqueIDs1:

      let uniqueIDs1 = [1,2,3,4,5,6];
      let uniqueIDs2 = [1,3,4,6,10];
      let uv1 = uniqueIDs1.filter(x => !uniqueIDs2.includes(x));
      let uv2 = uniqueIDs2.filter(x => !uniqueIDs1.includes(x));
      let uv = uv1.concat(uv2);
      console.log(uv) // [ 2, 5, 10 ]