find unique elements in array using ecmascript code example
Example 1: array unique values javascript
const myArray = ['a', 1, 'a', 2, '1'];
const unique = [...new Set(myArray)];
Example 2: javascript get distinct values from array
const categories = ['General', 'Exotic', 'Extreme', 'Extreme', 'General' ,'Water', 'Extreme']
.filter((value, index, categoryArray) => categoryArray.indexOf(value) === index);
This will return an array that has the unique category names
['General', 'Exotic', 'Extreme', 'Water']
Example 3: how to get unique values from array in javascript without duplicate value
function uniqueArray2(arr) {
var a = [];
for (var i=0, l=arr.length; i<l; i++)
if (a.indexOf(arr[i]) === -1 && arr[i] !== '')
a.push(arr[i]);
return a;
}