for example if arr = [1,1,2,3,5,5]
this will return [1,2,3,5]
.
function returnUnique(arr) {
let unique = [...new Set(arr)];
return unique;
};
my goal is to return only 2,3
for example if arr = [1,1,2,3,5,5]
this will return [1,2,3,5]
.
function returnUnique(arr) {
let unique = [...new Set(arr)];
return unique;
};
my goal is to return only 2,3
function returnUnique(arr) {
return arr.filter(e => arr.indexOf(e) === arr.lastIndexOf(e));
}
console.log( returnUnique([1,1,2,3,5,5]) );
Use reduce
function, for example:
const array = [1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6];
const uniqueValues = (array) => array.reduce((arr, item) => {
if (!arr.some((filteredItem) =>
JSON.stringify(filteredItem) === JSON.stringify(item)
))
arr.push(item);
return arr;
}, []);
console.log(uniqueValues(array))