1.reduce--让数组中的前项和后项做某种计算,并累计最终值
let arr = [1,2,3,4,5];
let result = arr.reduce((prev, cur, index, arr)=> {
return prev + cur;
});//15
var result = [
{
subject: 'math',
score: 88
},
{
subject: 'chinese',
score: 95
},
{
subject: 'english',
score: 80
}
];
var sum = result.reduce(function(prev, cur) {
return cur.score + prev;
}, 0);//第二个参数就是设置prev的初始类型和初始值,比如为0,就表示prev的初始值为number类型,值为0,因此,reduce的最终结果也会是number类型。
2.some---只要其中一个为true 就会返回true.3.every---全部满足条件才会返回true.
/**
* some是只要有一个符合条件就是true,every是全部符合才为true
*/
var computers = [
{name:"Apple",ram:8},
{name:"IBM",ram:4},
{name:"Acer",ram:32},
];
var result= computers.every(function(computer){
return computer.ram > 16
})
console.log(result)//false;
var some = computers.some(function(computer){
return computer.ram > 16
})
console.log(some)//true;
4.filter--过滤筛选符合条件的元素,但是不会修改原数组
var arr = [20,30,50, 96,50]
var newArr = arr.filter(item => item>40)
console.log(newArr)//[50, 96, 50]