方式1:
const unique = (value, index, arr) => {
return arr.indexOf(value) === index;
}
const sampleValues = [1, 4, 5, 2, ‘a‘, ‘e‘, ‘b‘, ‘e‘, 2, 2, 4];
const uniqueValues = sampleValues.filter(unique);
5
1
const unique = (value, index, arr) => {
2
return arr.indexOf(value) === index;
3
}
4
const sampleValues = [1, 4, 5, 2, ‘a‘, ‘e‘, ‘b‘, ‘e‘, 2, 2, 4];
5
const uniqueValues = sampleValues.filter(unique);
方式2:
const sampleValues = [1, 4, 5, 2, ‘a‘, ‘e‘, ‘b‘, ‘e‘, 2, 2, 4];
const uniqueValues = [...new Set(sampleValues)];
1
const sampleValues = [1, 4, 5, 2, ‘a‘, ‘e‘, ‘b‘, ‘e‘, 2, 2, 4];
2
const uniqueValues = [new Set(sampleValues)];
作者知乎/公众号:前端疯 (一群热爱前端的一线程序员维护,想要用前端改变世界。)