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 myArray = ['a', 1, 'a', 2, '1'];
const unique = [new Set(myArray)]; // ['a', 1, 2, '1']
xxxxxxxxxx
const a = [1, 9, 2, 2, 3, 4, 1, 7, 8, 0, 9, 0, 1, 5, 3];
const b = a.filter(function (item, index, array) {
return array.lastIndexOf(item) === index; // this will return the unique elements
});
xxxxxxxxxx
const arr1=[1,2,3,4,5,5];
const unique = arr1.filter((item, index, array)=>
array.indexOf(item)===index
)
console.log(unique)
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
<?php
// app.php
$data = [19, 21, 19, 21, 46, 21, 29, 21, 18];
print_r(array_unique($data));
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
const array = [2,5,7,5,6,4,2,4,5,8,412,477,36,8,2,34,7]
const generateUniqueArray => arr => [ new Set(arr)]
const result = generateUniqueArray(array)
console.log(result) // [ 2, 5, 7, 6, 4, 8, 412, 477, 36, 34]
// With love @kouqhar