js 数组去重

数组去重

1 Set

const arr=[...new Set([1,2,2,3])]
const arr=Array.from(new Set([1,2,2,3]))

2 Map

const arr = [1, 1, 2, 2, 3, 4, 5];
const uniqueArray = [...new Map(arr.map(item => [item, item])).values()];

3 filter

const arr = [1, 1, 2, 2, 3, 4, 5];
const uniqueArr = arr.filter((value, index, self) => {
return self.indexOf(value) === index;
});

4 reduce

const arr =  [1, 1, 2, 2, 3, 4, 5];
const uniqueArray = arr.reduce((acc, cur) => {
if (!acc.includes(cur)) {
acc.push(cur);
}
return acc;
}, []);