var arr = [
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6]
]
var sum = new Array(arr[0].length)
sum.fill(0); //初始化sum
for(var i = 0; i < arr.length; i++) {
for(var j = 0; j < arr[0].length; j++) {
sum[j] += arr[i][j];
}
}
console.log(sum)
然后,看到了网上大哥们的奇淫巧技快速声明一个数据并初始化(记录一下):
- Array.prototype.fill(), [...Array(100).fill(0)]
- new Int8Array(100)
- Array.from({length: 100}, _ => 0)
- for(var a = [], n = 0; n < 100; arr[n++] = 0);
- new Array(101).join(0).split('').map((n) => +n)
- [...Array(100)].map(_=>0)
- 等等
... 注意一下兼容性
网友评论