JavaScript中常用的一些方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GreyBearChao/article/details/82966618

一. 数组

reduce() :对数组中的数据进行累计操作。

传入四个参数:累计值,数组每一项的值,索引,数组本身。

var arr = [1, 2, 3, 4];
var res = arr.reduce(function (total, val, index, arr){
  return total + val;
});
// 第一次的total值为数组第一项的值,即1,val为数组第二项的值,即2。
console.log('res', res); // 10

猜你喜欢

转载自blog.csdn.net/GreyBearChao/article/details/82966618