首页 > 其他分享 >二位数组、对象数组对应索引的值相加

二位数组、对象数组对应索引的值相加

时间:2022-11-23 11:22:11浏览次数:46  
标签:index 相加 索引 json let result 数组

二位数组、对象数组对应索引的值相加,如下列arr1、arr2、arr3索引 0 的数据相加 1 + 11 + 10 = 22

let arr1 = [1,2,3,4,5,6];
let arr2 = [11,12,13,14,15,16];
let arr3 = [10,21,31,41,51,61];
let json = {arr1,arr2,arr3};
//保存结果的数组
let result = [];  
//遍历json
for(let key in json){
  //遍历数组的每一项
  json[key].forEach((value,index) => {
      if (result[index] == undefined) {
        result[index] = 0
      }
      result[index] += value;
  })    
}
//打印结果
console.log(result); //[22, 35, 47, 59, 71, 83]

 

标签:index,相加,索引,json,let,result,数组
From: https://www.cnblogs.com/chensv/p/16917673.html

相关文章