多维数组我如何求和列的值?

时间:2019-06-04 12:33:49

标签: javascript

我完全被卡住了,我试图对数组的列求和,但是我尝试了6个小时,但找不到解决方法:(

我找到了汇总行而不汇总列的方法,但我不知道自己在做什么错。真令人沮丧。我将不胜感激地找到做到这一点的方法。

我没有使用索引0。所有值都从索引(1x1)row1 x column1开始存储,让最新的行和列为空以打印行之和和列之和

let row, qtyRow, qtyColumn, i, sumaRow, numberRow, sumaColumn, column

qtyRow = parseInt(prompt(`How many rows in the array?`))
qtyRow = qtyRow + 2
var mainTab = [qtyRow]

qtyColumn = parseInt(prompt(`How many columns in the array?`))
qtyColumn = qtyColumn + 2

/* Asignación de nuevo Array al Array principal. Poniendo las columnas a las filas */
for (i = 0; i < qtyRow; i++) {
  mainTab[i] = new Array(qtyColumn)
  console.log(mainTab)
}

/* Entry of numbers for rows and columns and the last column it left empty */
let counterRow = 1
for (row = 1; row < qtyRow - 1; row++) {
  for (column = 1; column < qtyColumn - 1; column++) {
    dataTable = parseInt(prompt(`Value of row ${(counterRow)} column ${(column)}?`))
    mainTab[row][column] = dataTable
  }
  counterRow++
}

/* printing numbers and adding rows*/
counterRow = 1
for (row = 1; row < qtyRow; row++) {
  document.write(`<br><b>Row ${counterRow} - </b>`)

  sumaRow = 0
  for (column = 1; column < qtyColumn; column++) {
    if (column === qtyColumn - 1) {
      document.write(" " + `<b>${sumaRow}</b>`)
    } else {
      document.write(mainTab[row][column] + " | ")
      sumaRow = sumaRow + mainTab[row][column]
    }
  }
  counterRow++
}

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您有两个长度不同的数组,并且想要生成第三个数组,其中每个元素将是其他数组的元素之和

const arr1 = [1, 2, 3, 4, 5];
const arr2 = [1, 2, 3];
// get the length
const [len1, len2] = [arr1.length, arr2.length];
// find the smallest and the largest array
const [small, large] = (len1>len2)?[arr2, arr1]:[arr1, arr2];
const arr3 = [];
let i;
// fill the output array until we reach the length of the smallest one
for(i = 0; i<small.length; ++i) arr3.push(small[i] + large[i]);
// fill the rest
for(; i<large.length; ++i) arr3.push(large[i]);

答案 1 :(得分:0)

我使用double reduce函数计算行和列总和

let row, qtyRow, qtyColumn, i, sumaRow, numberRow, sumaColumn, column

qtyRow = parseInt(prompt(`How many rows in the array?`))
var mainTab = [qtyRow]

qtyColumn = parseInt(prompt(`How many columns in the array?`))
qtyColumn = qtyColumn

/* Asignación de nuevo Array al Array principal. Poniendo las columnas a las filas */
for (i = 0; i < qtyRow; i++) {
  mainTab[i] = new Array(qtyColumn)
}


/* Entry of numbers for rows and columns and the last column it left empty */
let counterRow = 1
for (row = 0; row < qtyRow; row++) {
  for (column = 0; column < qtyColumn; column++) {
    dataTable = parseInt(prompt(`Value of row ${counterRow + 1} column ${column + 1}?`))
    mainTab[row][column] = dataTable
  }
  counterRow++
}


// Calculating the sums of rows and cols

let rowsums = [], colsums = [];
mainTab.reduce((colsums, row, indexRow, source) => {
  row.reduce((rowsums, col, indexCol, source2) => {
    rowsums[indexRow] = (rowsums[indexRow] || 0) + col
    colsums[indexCol] = (colsums[indexCol] || 0) + col
    return rowsums
  }, rowsums)
  return colsums
}, colsums)

// Output table and sums as <table>
document.write('<table border="1">');

mainTab.forEach((row, index) => {
  
  document.write(`<tr><th>Row ${index + 1}:</th>`);
  row.forEach((col, index2) => {
    document.write(`<td>${col}</td>`)
    // console.log(row, index, col, index2)
  })
  document.write(`<th>${rowsums[index]}</th></tr>`)
})

document.write(`<tr><th>Sum:</th>`);
colsums.forEach((col, index2) => {
  document.write(`<th>${col}</th>`)
  // console.log(row, index, col, index2)
})
document.write('<th>' + colsums.reduce((accu, col) => accu + col, 0) + "</th>")

document.write('</table>');

相关问题