welcome

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

二位数组、对象数组对应索引的值相加,如下列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]

 

posted @ 2022-11-23 11:14  一首弦曲献仙音  阅读(27)  评论(0编辑  收藏  举报
//博文图片放大缩小 //飘桃花效果