文章来源
Set 与去重
- 数组去重
const arr = [3, 5, 2, 2, 5, 5];
const unique = [...new Set(arr)];
- 数组去重函数
function unique(array) {
return [...new Set(array)];
}
- 字符去重
let str = [...new Set("ababbc")].join("");
console.log(str);
数组求和
let foo = [1, 2, 3, 4, 5];
//不优雅
function sum(arr) {
let x = 0;
for (let i = 0; i < arr.length; i++) {
x += arr[i];
}
return x;
}
sum(foo); // => 15
//优雅
foo.reduce((a, b) => a + b); // => 15