On the problem of calculating array corresponding subscript Array

let list= [["",12,1,8],["",5,0,0],["",1,0,0],["",10,0,20],["",0,10,0],["",0,50,0]]

//
{"0":"","1":28,"2":61,"3":28}
Jan.19,2022

reduce is more semantic in this situation

let list = [['', 12, 1, 8], ['', 5, 0, 0], ['', 1, 0, 0], ['', 10, 0, 20], ['', 0, 10, 0], ['', 0, 50, 0]];

let result = list.reduce((res, arr) => {
  arr.forEach((num, index) => {
    if (index) {
      res[index] = (res[index] || 0) + num;
    }
  });
  return res;
}, {'0': ''});

console.log(result);

the answer on the first floor is very good.

simple scheme:

let list= [
    ["",12,1,8],
    ["",5,0,0],
    ["",1,0,0],
    ["",10,0,20],
    ["",0,10,0],
    ["",0,50,0]
];

const handleData = (data) => {
    // 
    let maxIndex = data.map(item => item.length).sort((a,b)=> b-a)[0];
    
    // 
    let resArr = new Array(maxIndex).fill(0);
    
    // 
    data.forEach( item => {
        item.forEach( (v, i) => {
          resArr[i] += (v || 0)
        })
    });
    
    // res
    let res = {};
    resArr.forEach( (v, i)=> {
     res[i] = i == 0 ? '' : v
    });
    
    return res;
}

console.log( handleData(list));

run result:
clipboard.png


put the data and test this should be the fastest

var list= [["",12,1,8],["",5,0,0],["",1,0,0],["",10,0,20],["",0,10,0],["",0,50,0]];
function test2(list){
 var obj = {'0':''};
 for(var x=0;x<list.length;xPP){
   for(var i=0;i<list[x].length;iPP)
    i>0&&(obj[i]=obj[i]==null?list[x][i]:obj[i]+list[x][i]);
}
  return obj;
}
test2(list);
Menu