将二维数组中的所有数组元素与一个数字相乘



function multiplyer(...arr){
let res=[],product=1;

for(var j=arr.length-1;j>=0;j--){

res.unshift(arr[j].map(item=>item*product))
product*=10
}
return res;
}
console.log(multiplyer([[2,3][3,5]]))

我期待着类似[[20,30][3,5]]的东西,但我认为我在访问2d数组的元素时遇到了问题。结果为[ [ NaN ] ]

对于一个问题,[ [ 2, 3 ][ 3, 5 ] ]需要一个额外的逗号使其成为2d数组:[ [ 2, 3 ], [ 3, 5 ] ]

另一个问题是函数定义中的一个不必要的扩展算子(...(混淆了函数。

function multiplyer(arr) {
let res = [],
product = 1;
for (var j = arr.length - 1; j >= 0; j--) {
res.unshift(arr[j].map(item => item * product))
product *= 10;
}
return res;
}
console.log(multiplyer([
[2, 3],
[3, 5]
]))

最新更新