how to find unique elements in an array of objects in js code example

Example 1: array unique values javascript

const myArray = ['a', 1, 'a', 2, '1'];
const unique = [...new Set(myArray)]; // ['a', 1, 2, '1']

Example 2: javascript get unique values from array

const myArray = [1,2,3,1,5,8,1,2,9,4];
const unique = [...new Set(myArray)]; // [1, 2, 3, 5, 8, 9, 4]

const myString = ["a","b","c","a","d","b"];
const uniqueString = [...new Set(myString)]; //["a", "b", "c", "d"]

Example 3: get only unique values from array javascript

arr = [2, 2, 2, 1, 3, 3, 3,3, 4, 5];
arr = arr.sort().filter((item,i)=>!(arr[i] == arr[i+1] || arr[i-1]==arr[i]));
// this will remove all item which are repeating more then one
console.log(arr);
// [1, 4, 5]

Tags:

Php Example