//以前的用法const nums =[10,20,111,222,4444,40,50]//取出所有小于100的数字let newNums =[]//1for(let i of nums){if(n <100){
newNums.push(i)}}//2将所有的newNum数字相加,得到最终结果let total =0;for(let n of newNums){
total += n
}
//需求:将原来数组的元素都*2//map会遍历调用它的数组,并对其每一个元素做改变let new2Nums = newNums.map(function(n){return n *2})
console.log(new2Nums)//输出[20, 40, 80, 100]
reduce()会遍历调用他的数组对象,并做进一步操作,返回一个值
//传两个参数 一个是回调函数,第二个值为pruValue的初始化值(若不写的话默认为0)let total = new2Nums.reduce(function(preValue, n){return preValue + n
},0)//第一次:preValue:0 n:20
total用高阶函数怎么算
let total = nums.filter(function(n){return n <100}).map(function(n){return n *2}).reduce(function(preValue, n){return preValue + n
})
console.log(total)//输出240//更简洁的写法let total = nums.filter(n => n <100).map(n => n *2).reduce((pre, n)=> pre + n )
console.log(total)//输出为240