JavaScript || How To Get All Unique Values In An Array & Remove Duplicates Using Vanilla JavaScript

Print Friendly, PDF & Email

The following is a module with functions which demonstrates how to get all distinct values in an array & remove duplicates from a simple and object array.

The functions on this page allows for an optional selector function as a parameter, which makes them general enough to work on arrays of any type.


1. Simple Array

The example below demonstrates the use of ‘Utils.arrayUnique‘ to get unique values from a simple array.


2. Object Array

The example below demonstrates the use of ‘Utils.arrayUnique‘ to get unique values from an object array.

In this example, a selector is used, which extracts the value to be used in the evaluation.


3. Mixed Array

The example below demonstrates the use of ‘Utils.arrayUnique‘ to get unique values from a mixed array.

In this example, a selector is used, which extracts the value to be used in the evaluation.


4. Utils Namespace

The following is the Utils.js Namespace. Include this in your project to start using!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Leave a Reply