2020年11月14日 学习笔记

2020年11月14日 学习笔记

forEach,reduce,map,filter 函数

forEach 函数

遍历数组,使得其中每一项执行某个操作

let arr = [1,2,3,4,5];
arr.forEach((item,index)=> {
    console.log(item);
})
reduce 函数

让数组的前后项做某种计算,并累计最终值
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

  • initialValue: 传递给数组的初始值,非必填;
  • total:累计器累计回调的返回值; 它是上一次调用回调时返回的累积值
  • currentValue:数组中正在处理的元素
  • currentIndex:数组中正在处理的当前元素的索引,非必填
  • arr:调用reduce()的数组,非必填
var newArr = [15.5, 2.3, 1.1, 4.7].reduce(function(total,num){
	return total + Math.round(num);//对数组元素进行四舍五入并计算总和
}, 0);
console.log(newArr);//24

map 函数

对数组的每一项进行同一个操作,将得到值组成一个新数组

let arr = [1,2,3,4,5];
let newArray = arr.map((item,index)=> {
    return item * 2
});
newArray  // [2, 4, 6, 8, 10]
arr // [1,2,3,4,5]

filter 函数

返回符合条件的数组,不改写原数组

let array = [1,2,3,4].filter((item,index)=>{
    return item > 2;
})
console.log(array); // [3,4]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值