xxxxxxxxxx
// Example array
const array = [1, 2, 2, 3, 4, 4, 5, 6, 6];
// Using Set to get unique values
const uniqueValues = [new Set(array)];
console.log(uniqueValues); // Output: [1, 2, 3, 4, 5, 6]
xxxxxxxxxx
var arr = [55, 44, 65,1,2,3,3,34,5];
var unique = [new Set(arr)]
//just var unique = new Set(arr) wont be an array
xxxxxxxxxx
let a = ["1", "1", "2", "3", "3", "1"];
let unique = a.filter((item, i, ar) => ar.indexOf(item) === i);
console.log(unique);
xxxxxxxxxx
// usage example:
var myArray = ['a', 1, 'a', 2, '1'];
var unique = myArray.filter((v, i, a) => a.indexOf(v) === i);
// unique is ['a', 1, 2, '1']
xxxxxxxxxx
const myArray = ['a', 1, 'a', 2, '1'];
const unique = [new Set(myArray)]; // ['a', 1, 2, '1']
xxxxxxxxxx
const unique = (value, index, self) => {
return self.indexOf(value) === index
}
const ages = [26, 27, 26, 26, 28, 28, 29, 29, 30]
const uniqueAges = ages.filter(unique)
console.log(uniqueAges)
xxxxxxxxxx
const array = [1, 2, 3, 4, 4, 5, 3, 2];
// Using Set to remove duplicates
const uniqueArray = [new Set(array)];
console.log(uniqueArray);
xxxxxxxxxx
// one liner solution to get a unique array by object id
[{_id: 10},{_id: 20}, {_id: 20}].filter((item, i, ar) => ar.findIndex(each => each._id === item._id) === i)
xxxxxxxxxx
const array = [1, 2, 3, 1, 2, 4, 5, 3, 6, 4];
const uniqueElements = [new Set(array)];
console.log(uniqueElements);