javascript高阶函数的使用
#map
const app = new Vue({
el:'#app',
data:{
message:"hello,vue",
arr:[10,50,55,62,88,455,551,254,100,65,99]
},
computed:{
nums(){
//1.需求:获取小于100的数字
// filter 回调函数有一个要求:必须返回boolean 返回true,函数内部会自动将这次回调的n加入到新的数组中
// 返回false,函数内部会自动过滤掉这次的n
// let newArr = this.arr.filter(function (n) {
// return n < 100
// })
// console.log(newArr)
// //2.需求:从小于一百的数字里面获取新数组,然后乘以2
// //map 回调函数中的m是每项的值
// let new2Arr = newArr.map(function (m) {
// return m * 2
// })
// console.log(new2Arr);
// //3.需求:将乘以2的新数组结果相加
// // reduce 将数组中的所有数据进行汇总 prevalue 上一次返回的数据;n是遍历的值
// let total = new2Arr.reduce(function (preValue,n) {
// return preValue + n
// },0)
//连续使用
// let total = this.arr.filter(function (n) {
// return n < 100
// }).map(function (n) {
// return n*2
// }).reduce(function (preValue,n) {
// return preValue + n
// })
// 箭头函数
let total = this.arr.filter(n => n < 100).map(n => n*2).reduce((preValue,n)=> preValue + n)
console.log(total);
}
}
})