JavaScript reduce() 方法

this.data = this.list.reduce((total,per)=>{
  total.push({
    name:per.keyWrdDsc, 
    value:per.num1
  }}
  return total;
},[]);


this.config = ['bar','line','per'];
this.config.reduce((total,per)=>{
  total[per] = true;
},{})

res = {bar:true,line:true,pei:true}

实例

计算数组元素相加后的总和:

var numbers = [65, 44, 12, 4];
function getSum(total, num) {
  return total + num;
}
function myFunction(item) {
  document.getElementById("demo").innerHTML = numbers.reduce(getSum);
}

输出结果:

125

原文地址:https://www.cnblogs.com/ygyy/p/14202692.html