const users = [{name:'张三', age:30},{name:'李四', age:35},{name:'王5', age:12}]; const result = users.filter(user => user.age > 20); // [{name:'张三', age:30},{name:'李四', age:35}]
const users = [{name:'张三', age:30},{name:'李四', age:35},{name:'王5', age:12}]; const found = users.find(user => user.age > 20); // {name:'张三', age:30}
let userLists = ['张三','jaychou', '张三', '李四']; let users = [...new Set(userLists)]; // ['张三','jaychou', '李四'];
const users = [{name:'张三', age:30},{name:'李四', age:35},{name:'王5', age:12}]; Math.max.apply(Math, users.map(user => user.age)) // 35 最小值只需要把max改成min 或 Math.max(...users.map(i=>i.age)) // 35 最小值只需要把max改成min
//some()判断数组中是否有元素满足条件 //every()判断是否所有元素都满足条件 const users = [{name:'张三', age:30},{name:'李四', age:35},{name:'王5', age:12}]; users.some(user => user.age<20); // true users.every(user => user.age<20); // false
简单用法:求和和乘积 var arr = [1, 2, 3, 4]; var sum = arr.reduce((x,y)=>x+y) var mul = arr.reduce((x,y)=>x*y) console.log( sum ); //求和,10 console.log( mul ); //求乘积,24
复杂参考:JS数组reduce()方法详解及高级技巧 - 简书