Get Unique Values from Array


What is a Unique Array?

Just as straight forward as it sounds, it's an Array where all of the values within it are unique - meaning, there are no duplicated values within the Array.

Why would you need a Unique Array?

When you're working with Arrays, you're typically looping through them to do something with the values. Perhaps you're going to manipulate or transform each value in a certain way, use them to connect with another data set, or maybe even to write each array value into something like a Google Sheet. No matter what you plan to do with the Array, it'll typically be the case that you don't want to have duplicated values in it.

How to create a Unique Array

Let's take this array of IDs as an example:

let array = ['20240217094742', '03202417094803', '170220240902125', '170220248860209', '20240217094906', '20240217094742', '170220248860209', '4920240217090243', '170220240902125']

It's a bit hard to tell by eye, but there are 3 IDs that have been duplicated in it: 170220248860209, 170220240902125, and 20240217094742.

When you only have a handful of values in your array, it's not too troublesome to clean it up manually. However, it's seldom the case that you can be so lucky to only have a small data set nowadays. If you had to deduplicate thousands, hundreds, or even tens of values - you'd need a nice long mental break afterwards. No worries though, we can write a super simple script to do this for us.

In order to remove the duplicates using a script - you'd basically want to do something where you iterate through each value in the Array and put it into a new Array if it doesn't already exist in it. To do that, you can have a loop statement like this:

let newArray = [] for(item in array){ if(!newArray.includes(array[item])) { newArray.push(array[item]) } }

We're using Array.includes() to see if the new Array we created contains the value and we add an exclaimation mark in front of it to negate it - aka to see if the new Array doesn't contain the value we're focused on.

This isn't a bad way to do it, but there's a much more efficent and clean way to quickly create a unique array without having to use a loop statement.

What's the most efficient way to make a Unique Array?

In javascript, you can use something called Set() which helps with creating a unique set of data out of an existing data set. By combining this with the 'Spread' operator (i.e. ...) and putting it into an Array, we are able to return back the Set of values as an Array of unique values.

let newArray = [...new Set(array)]

This method of creating a unique array is not only easier to read, but is also more performant than using .includes()



How to use this code snippet

The code snippet below is a pre-built function that you can pop into your code and use freely without having to type out the [...new Set(array)] 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 createUniqueArray(data) { return [...new Set(data)] }

And to use it, you can just do this:

let result = createUniqueArray(array)
thumbnail

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