xxxxxxxxxx
var newArray = evenNumbers.filter((value, index, self) => self.indexOf(value) === index);
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
// 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);
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
// app.js
Array.prototype.unique = function() {
let arr = [];
for(let i = 0; i < this.length; i++) {
if(!arr.includes(this[i])) {
arr.push(this[i]);
}
}
return arr;
}
const ages = [26, 27, 26, 26, 28, 28, 29, 29, 30]
const uniqueAges = ages.unique()
console.log(uniqueAges)