Published on

Returning Unique Array Elements in JavaScript

Authors

Let's say you've an array and you want to return a unique element from it. Unfortunately, there is no built-in way to do it in JavaScript.

Here is how to return unique elements from an array using unique-random-array package.

Dependency

npm install unique-random-array

Snippet

import uniqueRandomArray from 'unique-random-array';

const random = uniqueRandomArray([1, 2, 3, 4, 5]);

console.log(random(), random(), random(), random()); // 1 3 4 2

How does it work?

Under the hood, it uses unique-random package to return a unique array element.

Happy returning elements!