1. 程式人生 > >掌握JavaScript基礎-過濾數組的重復值

掌握JavaScript基礎-過濾數組的重復值

lin don dex 筆記 utl check log keyword www.

方式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)];
作者知乎/公眾號:前端瘋 (一群熱愛前端的一線程序員維護,想要用前端改變世界。)
技術分享圖片


來自為知筆記(Wiz)

掌握JavaScript基礎-過濾數組的重復值