Create a Unique Array in JavaScript

Learn how to create a unique array in JavaScript with Set, filter(), and findIndex() examples, including how to remove duplicate objects by key.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Create a Unique Array in JavaScript

A unique array in JavaScript contains each value only once. This is useful when you want to remove duplicate primitives, clean input data, or prepare a list for display, comparison, or storage.

You can build a unique array with Set, filter(), and findIndex(). When you are normalizing text values first, JavaScript trim is often the next step before you dedupe the array.

Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.


Quick reference

Data Approach
Primitives [...new Set(arr)]
Order-preserving without Set filter + indexOf
Objects by field filter + findIndex on stable key

Method 1: Remove duplicates with Set

Set stores only unique values, so it is the shortest way to remove duplicate primitive values from an array.

javascript
const values = [1, 2, 3, 3, 4, 4, 5];
const unique = [...new Set(values)];

console.log("unique-array:", unique.join(","));
Output

You should see one line logging unique-array: 1,2,3,4,5.

Use this method when the array holds strings, numbers, booleans, or other primitive values.


Method 2: Remove duplicates with filter() and indexOf()

The callback keeps only the first position of each value, so later duplicates are dropped.

javascript
const values = ["a", "e", 12, 45, 78, 78, "a"];
const unique = values.filter((value, index, self) => self.indexOf(value) === index);

console.log("unique-filter:", unique.join(","));
Output

You should see one line logging unique-filter: a,e,12,45,78.

This approach is easy to read and works well when you want a simple unique array without introducing another data structure.


Method 3: Remove duplicate objects by key

Set does not remove duplicate object literals by value because each object is a different reference. For object arrays, use a stable key such as id and keep the first match.

javascript
const items = [
  { id: 1, name: "A" },
  { id: 2, name: "B" },
  { id: 1, name: "A-duplicate" }
];

const unique = items.filter((item, index, self) => index === self.findIndex((entry) => entry.id === item.id));

console.log("unique-object-ids:", unique.map((item) => item.id).join(","));
console.log("unique-object-names:", unique.map((item) => item.name).join(","));
Output

You should see 2 lines, in order: unique-object-ids: 1,2, unique-object-names: A,B.

Use this pattern when you need a unique array of objects for tables, menus, reports, or API data cleanup.


Summary

A unique array in JavaScript is easy to build when you match the method to the data type. Use Set for primitive values, filter() with indexOf() for a readable array-only solution, and filter() with findIndex() when you need to dedupe objects by a field such as id.


Official documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …