function forLoopItems(data) {
for(let item in data) {
console.log(data[item])
}
}
// forLoopItems([1,2,3,4,5,6,7])
When you're working with multiple data sets, you might run into a situation where you'll want to just grab the difference. A common scenario would be if you were combining two email distribution lists together and wanted have personalized messages depending on which email list your user/customer was in (or wasn't in).
Let's take these two arrays for example:
let array1 = [1,2,3,1,3]
let array2 = [3,4,5,5,4,5]
To figure out which values don't overlap across the two data sets, you'll want to iterate through each value in each array and see if it exists in the other array. At first glance, you might think to use a nested loop (aka a loop within a loop), but that's not going to be very efficient and it would also be hard to manage that code.
Instead, you can use the javascript Array.filter()
method which lets you quickly find the values within an Array that meet certain criteria. For the criteria, we'll make use of a negated includes()
statement (i.e. !array.includes(value)
) which will let us find the values that aren't found in the array.
let array1_unique = array1.filter(x => !array2.includes(x))
console.log('array1_unique', array1_unique);
let array2_unique = array2.filter(x => !array1.includes(x))
console.log('array2_unique', array2_unique);
This should give you an output of:
array1_unique: [ 1, 2, 1 ]
array2_unique: [ 4, 5, 5, 4, 5 ]
Now that you can find the unique for each array, you'll want to combine them into one array so that you can present the full difference between the two arrays. The first step is put the two arrays with this unique values into one array. There are two ways to do this:
// Method one - using "new Array"
let combined_diff = new Array(array1_unique, array2_unique)
// Method two - using square brackets
let combined_diff = [array1_unique, array2_unique]
In either method, the returned value will be:
combined_diff: [ [ 1, 2, 1 ], [ 4, 5, 5, 4, 5 ] ]
This is what you'd call a nested array, aka an array with a set of arrays within it. In order to use this array efficiently, we'll need to flatten it out using Array.flat()
like this:
let flattened_diff = combined_diff.flat()
Which gives you back this:
flattened_diff: [ 1, 2, 1, 4, 5, 5, 4, 5 ]
It's still not that pretty though, since we have duplicated values. We'll use the javascript Set()
method and Spread (i.e.g ...
) operator to create a unique set of these values.
let set_diff = [...new Set(flattened_diff)]
// Output: [ 1, 2, 4, 5 ]
Check out our code snippet for Creating Unique Arrays.
This works great for normal arrays, but what about nested arrays? Let's take this one for example:
let array1 = [1,2,3,[1,3]]
let array2 = [3,4,5,[4,5]]
If you ran those arrays through the above logic, you'd end up with this:
[ 1, 2, [ 1, 3 ], 4, 5, [ 4, 5 ] ]
Which isn't ideal. There's an easy way to fix that though -- just call flat at the very beginning before you star your logic! And just in case you have more than one level of nested data, you can use Array.flat(Infinity)
to make sure you catch all of them.
This logic has a bunch of lines of code in it, so we can actually shorten that up a bunch to make it more manageable.
[...new Set([array1.filter(x => !array2.includes(x)), array2.filter(x => !array1.includes(x))].flat())]
The code snippet below is a pre-built function that you can pop into your code and use freely without having to type out this complex line of code over and over again. To use it, simply copy and paste the function into your code and then call it wherever you need.
Here's the code snippet to copy:
function diffArrays(array1, array2) {
array1 = array1.flat(Infinity), array2 = array2.flat(Infinity)
return [...new Set([array1.filter(x => !array2.includes(x)), array2.filter(x => !array1.includes(x))].flat())]
}
And to use it, you can just do this:
let result = diffArrays(array1, array2)
NitroGAS Chrome Extension
Want to reference this code snippet right from your Google Apps Script Project? Check out our NitroGAS Chrome Extension. This tool will help you build your scripts faster than you could imagine. The tool itself and access to the code snippets are 100% FREE - Happy Coding!
Get the Extension