Get unique value by comparing 2 arrays

563 views
Skip to first unread message

CK Ng

unread,
Jun 21, 2022, 9:16:11 PM6/21/22
to Google Apps Script Community
Hi, im new for apps script, i would like to ask how to get the unique value by comparing 2 arrays, for example array1 is [ab,abc,abcd,def,defg], array2 is [abc,abcd,defg], i want to compare this 2 arrays and then get the value that doesnt exist in both array('ab' and 'def').

Appreciate for help.

R Tichy

unread,
Jun 21, 2022, 10:34:41 PM6/21/22
to Google Apps Script Community
let a = ['ab', 'abc', 'abcd', 'def', 'defg']
let b = ['abc', 'abcd', 'defg'];
var result = [];
Logger.log(a, b, result)
a.forEach( function(each){
   if (!b.includes(each)) { result.push(each);
}});
b.forEach(function(each){
   if (!a.includes(each)) { result.push(each);}
});
Logger.log(result);
Message has been deleted

dimud...@gmail.com

unread,
Jun 22, 2022, 6:08:38 AM6/22/22
to Google Apps Script Community

You're looking for a 'diff' algorithm; ie. the difference of two sets returns the values that are not common to both.

There are lots of implementations out there on the web but some are more performant than others.

If performance is paramount, your best bet is to use libraries like Lodash or Ramda (granted, porting libraries to GAS can be difficult but I believe there is a lodash port available for GAS).

If you value readability over performance check out the code from the article linked below on set operations in modern Javascript:

https://2ality.com/2015/01/es6-set-operations.html

Keith Andersen

unread,
Jun 22, 2022, 10:57:01 AM6/22/22
to google-apps-sc...@googlegroups.com

let a = ['ab', 'abc', 'abcd', 'def', 'defg']
let b = ['abc', 'abcd', 'defg'];

let result = a.concat(b);

//console.log(result);

let uniqueArray = result.filter((item, index) => result.indexOf(item) === index);

uniqueArray.sort();

console.log(uniqueArray);


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/76a7242e-9361-480f-976c-b1203438fdc3n%40googlegroups.com.

Bruce Mcpherson

unread,
Jun 22, 2022, 11:28:54 AM6/22/22
to google-apps-sc...@googlegroups.com
const x = () => {
const a1 = [1,2,3,4,9,20]
const a2 = [2,20,3,4,8,7]

// a)
const resulta = a1.concat(a2).filter(f=>!(a2.includes(f)&&a1.includes(f)))
// [1,9,8,7]
console.log(resulta)

// b)
const a1Set = new Set(a1)
const a2Set = new Set(a2)

const resultb = a1.filter(f=>!a2Set.has(f)).concat(a2.filter(f=>!a1Set.has(f)))
// [1,9,8,7]
console.log(resultb)

}

On Wed, 22 Jun 2022 at 02:16, CK Ng <ckng...@gmail.com> wrote:
Hi, im new for apps script, i would like to ask how to get the unique value by comparing 2 arrays, for example array1 is [ab,abc,abcd,def,defg], array2 is [abc,abcd,defg], i want to compare this 2 arrays and then get the value that doesnt exist in both array('ab' and 'def').

Appreciate for help.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages