Get All Unique Values In A JavaScript Array (remove Duplicates)

To get all unique values in a JavaScript array and remove duplicates, you can use the Set object. Here’s an example:

const array = [1, 2, 2, 3, 4, 4, 5];

const uniqueArray = [ Set(array)];

console.log(uniqueArray); // Output: [1, 2, 3, 4, 5]

In the above example, a Set object is created using the new Set(array) syntax, where array is the original array. The spread operator ... is used to convert the Set object back into an array. This results in removing any duplicate values and creating a new array uniqueArray with only the unique values.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!