尝试添加二维数组的所有元素

Trying to add all the elements of two-dimensional array

sum([[1]]) returns 1,但是 sum([[1, 2], [3]]) 不是 return 6,而是 1。我是在这里找不到问题我是编程新手

function sum(d) {
let into = 0;
for (let i = 0; i < 5; i++) {
    for (let j = 0; j < 5; j++) {
        into = into + d[i][j];
        return into
    }
}

}

事情是这样的:您需要遍历两个数组并选取元素。

let d =[[1,2],[3]]


function sum(d) {
let into = 0;
for (let i = 0; i < d.length; i++) {
    for (let j = 0; j < d[i].length; j++) {
        into += d[i][j];
        
      
    }
}
  console.log(into)
 }  

sum(d)