数组去重
const arr = [1, 2, 3, 2, 4, 3, 3, 2, 5]; console.log([...new Set(arr)]); // [1, 2, 3, 4, 5] const productList = [{ name: 'a', category: '衬衫' }, { name: 'b', category: '卫衣' }, { name: 'd', category: '衬衫' }, { name: 'f', category: '短袖' }, { name: 'e', category: '短袖' }] console.log([...new Set(productList.map(product => product.category))]); //["衬衫", "卫衣", "短袖"]点击复制复制失败已复制
数组求和
const arr1 = [1, 2, 3, 2, 4, 3, 3, 2, 5]; const arr2 = [{ name: 'fs', age: 12 }, { name: 'fsdgdfg', age: 34 }, { name: 'ggg', age: 90 }] console.log(arr1.reduce((a, b) => a + b)); //25 console.log(arr2.reduce((a, b) => a + b.age, 0)); //136