JavaScript - 相同行和列中的值的总和

时间:2021-05-09 13:16:48

I have JavaScript array with numbers 0 and 1 and I need to make a sum of all numbers in same row and column (if I imagine my array in two dimensions). I want to create second array with sums for every single value in first array.

我有数字0和1的JavaScript数组,我需要在同一行和列中对所有数字求和(如果我想象我的数组在两个维度)。我想创建第二个数组,其中包含第一个数组中每个值的和。

2D array visualization for first item (X-Y table of values):
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
(Sum: 8 for value at index 0;0 (with value itself excluded))

第一项的二维数组可视化(XY值表):1,1,1,1,1 1,1,1,1,1 1,1,1,1,1 1,1,1,1,1 1 ,1,1,1,1(总和:8表示索引0处的值; 0表示(值本身不包括在内))

Real array I have:
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
(Sum: 8 for value at index 0)

真实阵列我有:1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1(总和:指数0的值为8)

2D array visualization for second item:
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1

(Sum: 8 for value at index 1;0)
This way I need to loop through whole array.

第二项的二维数组可视化:1,1,1,1,1 1,1,1,1,1 1,1,1,1,1 1,1,1,1,1 1,1,1,1 ,1(总和:索引1处的值为8; 0)这样我需要遍历整个数组。

2D array visualization for second item with zero separator:
1,1,1,1,1
1,1,1,1,1
1,0,1,1,1
1, 1 ,1,1,1
1, 1 ,1,1,1
(Sum: 5 for value at index 1;0)
Values after zero I don't want to count in.

具有零分隔符的第二项的2D阵列可视化:1,1,1,1,1 1,1,1,1,1 1,0,1,1,1 1,1,1,1,1,1,1, 1,1,1(总和:索引1处的值为5; 0)零之后的值我不想计入。

For previous table the result table should be like...
8,5,8,8,8
8,5,8,8,8
4,x,6,6,6
8,5,8,8,8
8,5,8,8,8

对于上表,结果表应该是...... 8,5,8,8,8 8,5,8,8,8 4,x,6,6,6 8,5,8,8,8 8, 5,8,8,8

Thank you very much for your help!

非常感谢您的帮助!

1 个解决方案

#1


2  

var res = [];  //the 1D array to hold the sums
var hArr =  [
   [ 1, 1, 1, 1 ],
   [ 1, 1, 1, 1 ],
   [ 1, 0, 0, 1 ],
   [ 1, 1, 0, 0 ]
]; //your array


var vArr = []; //Now lets create an array of arrays with the columns of hArr

for (var j=0; j<hArr[0].length; j++) {
  var temp = [];
  for (var i=0; i<hArr.length; i++) {
      temp.push(hArr[i][j]);
  }
  vArr.push(temp);
}

//sum all the element in the line - Vertically and Horizontally
function SumVH (hInd, vInd) {
  var sum = 0;
  //add horizontal elements
  //to the left
  for(var i=(vInd-1); i>=0; i--) {
    //if a 0 is found, break
    if (hArr[hInd][i] == 0) {
      break;
    }
    sum += hArr[hInd][i];
  }

  //to the right
  for(var i=(vInd+1); i<hArr[hInd].length; i++) {
    //if a 0 is found, break
    if (hArr[hInd][i] == 0) {
      break;
    }
    sum += hArr[hInd][i];
  }

  //add vertical elements
  //towards top
  for(var i=(hInd-1); i>=0; i--) {
    //if a 0 is found, break
    if(vArr[vInd][i] == 0) {
      break;
    }
    sum += vArr[vInd][i];
  }

  //towards bottom
  for(var i=(hInd+1); i<vArr[vInd].length; i++) {
    //if a 0 is found, break
    if(vArr[vInd][i] == 0) {
      break;
    }
    sum += vArr[vInd][i];
  }  
  //console.log("hInd="+hInd+" vInd="+vInd+" Sum="+sum);
  return sum;
}

// go through the main array and get result
var sumR = 0;
//sum of each row
for (var i=0; i<hArr.length; i++) {
   for (var j=0; j<hArr[i].length; j++) {    
      sumR = SumVH(i,j);
      res.push(sumR);
   }   
}

Please check it and let me know if it is working as you expect it to work. res variable holds the result.

请检查它,并告诉我它是否正常工作。 res变量保存结果。

#1


2  

var res = [];  //the 1D array to hold the sums
var hArr =  [
   [ 1, 1, 1, 1 ],
   [ 1, 1, 1, 1 ],
   [ 1, 0, 0, 1 ],
   [ 1, 1, 0, 0 ]
]; //your array


var vArr = []; //Now lets create an array of arrays with the columns of hArr

for (var j=0; j<hArr[0].length; j++) {
  var temp = [];
  for (var i=0; i<hArr.length; i++) {
      temp.push(hArr[i][j]);
  }
  vArr.push(temp);
}

//sum all the element in the line - Vertically and Horizontally
function SumVH (hInd, vInd) {
  var sum = 0;
  //add horizontal elements
  //to the left
  for(var i=(vInd-1); i>=0; i--) {
    //if a 0 is found, break
    if (hArr[hInd][i] == 0) {
      break;
    }
    sum += hArr[hInd][i];
  }

  //to the right
  for(var i=(vInd+1); i<hArr[hInd].length; i++) {
    //if a 0 is found, break
    if (hArr[hInd][i] == 0) {
      break;
    }
    sum += hArr[hInd][i];
  }

  //add vertical elements
  //towards top
  for(var i=(hInd-1); i>=0; i--) {
    //if a 0 is found, break
    if(vArr[vInd][i] == 0) {
      break;
    }
    sum += vArr[vInd][i];
  }

  //towards bottom
  for(var i=(hInd+1); i<vArr[vInd].length; i++) {
    //if a 0 is found, break
    if(vArr[vInd][i] == 0) {
      break;
    }
    sum += vArr[vInd][i];
  }  
  //console.log("hInd="+hInd+" vInd="+vInd+" Sum="+sum);
  return sum;
}

// go through the main array and get result
var sumR = 0;
//sum of each row
for (var i=0; i<hArr.length; i++) {
   for (var j=0; j<hArr[i].length; j++) {    
      sumR = SumVH(i,j);
      res.push(sumR);
   }   
}

Please check it and let me know if it is working as you expect it to work. res variable holds the result.

请检查它,并告诉我它是否正常工作。 res变量保存结果。