P粉8785105512023-08-24 00:43:53
The accepted answer is great and works 90% of the time.
However, if you are making a high-performance JS application and are using large/huge arrays, Array.map(..) will create a lot of load in terms of memory and processor usage, Because it creates a copy of the array.
I recommend using the classic for loop:
a = new Array(ARRAY_SIZE); for (var i = 0; i < ARRAY_SIZE; i++) { a[i] = []; } // 或者使用一行代码的替代方案 for (var i = 0, a = []; i < ARRAY_SIZE; a[i++] = []);
I tested six alternatives and got the following results:
Array.map(), as above (11 times slower than the original! ):
a = new Array(ARRAY_SIZE).fill().map(u => { return []; });
for loop, best choice (fastest):
// 标准的多行方式 a = new Array(ARRAY_SIZE); for (var i = 0; i < ARRAY_SIZE; i++) { a[i] = []; } // 一行语法 for (var i = 0, a = []; i < ARRAY_SIZE; a[i++] = []);
forEach (slower 6 times ):
a = new Array(ARRAY_SIZE).fill(); a.forEach((val, i) => { a[i] = []; })
[Updated on 2020-08-27] Ilias Karim proposed another method
Array.from (30 times slower! ) - Definitely worse in terms of performance, despite having the best syntax :(
a = Array.from({ length: ARRAY_SIZE }, () => []);
[..Array(..)] (5 times slower!)
a = [...Array(ARRAY_SIZE)].map(_=>([]))
Array.push(..), ranked second in terms of performance (2x slower! )
let a = [], total = ARRAY_SIZE; while(total--) a.push([]);
PS: I tested on this fiddle.
P粉4615998452023-08-24 00:21:01
You can first fill the array with any value (e.g. undefined
) and then you can use map
:
var arr = new Array(2).fill().map(u => ({}));
var arr = new Array(2).fill().map(Object);