Search code examples
listgoogle-apps-scriptcomparison

Compared lists in google apps script


I am trying to compare 2 lists and find elements in list1 that are NOT present in list2.

list1: [01-0161051, 01-0161053, 01-0161057, 01-0161058, 01-0161065, 01-0161067, 01-0161068]
list2: [01-0161051, 01-0161053, 01-0161057, 01-0161058, 01-0161065, 01-0161066]

Elements in list1 but not in list2 are [01-0161067, 01-0161068]

I tried this code but it does not yield expected results:

missing = null,
i = list1.length;
while(i) {
missing = ( ~list1.indexOf(list2[--i]) ) ? missing : list1[i];
}

Any leads would be appreciated.


Solution

  • var list1 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161067', '01-0161068'];
    var list2 = ['01-0161051', '01-0161053', '01-0161057', '01-0161058', '01-0161065', '01-0161066'];
    
    var missing = list1.filter(x => !list2.includes(x));
    
    console.log(missing); // [ '01-0161067', '01-0161068' ]