掌握JavaScript基礎-過濾數組的重復值
阿新 • • 發佈:2018-03-31
lin don dex 筆記 utl check log keyword www. 方式1:
來自為知筆記(Wiz)
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 1const unique = (value, index, arr) => {
2 return arr.indexOf(value) === index;
3}
4const sampleValues = [1, 4, 5, 2, ‘a‘, ‘e‘, ‘b‘, ‘e‘, 2, 2, 4];
5const uniqueValues = sampleValues.filter(unique);
方式2:const sampleValues = [1, 4, 5, 2, ‘a‘, ‘e‘, ‘b‘, ‘e‘, 2, 2, 4];
const uniqueValues = [...new Set(sampleValues)];
1const sampleValues = [1, 4, 5, 2, ‘a‘, ‘e‘, ‘b‘, ‘e‘, 2, 2, 4];
2const uniqueValues = [ new Set(sampleValues)];
作者知乎/公眾號:前端瘋 (一群熱愛前端的一線程序員維護,想要用前端改變世界。)來自為知筆記(Wiz)
掌握JavaScript基礎-過濾數組的重復值