1
問題是_.fill(Array(4), [])
等於[[], [], [], []]
?從lodash填充vs普通數組填充數組時出現不希望的結果
因爲_.isEqual(_.fill(Array(4), []), [[], [], [], []])
是true
var test = [[], [], [], []];
test[0].push(1);
console.log(test[0]);
test[1].push(2);
console.log(test[1]);
test[2].push(3);
console.log(test[2]);
test[3].push(4);
console.log(test[3]);
回報
[1]
[2]
[3]
[4]
這是我想要的,但
var test = _.fill(Array(4), []);
test[0].push(1);
console.log(test[0]);
test[1].push(2);
console.log(test[1]);
test[2].push(3);
console.log(test[2]);
test[3].push(4);
console.log(test[3]);
回報
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
我做錯了嗎?我想創建並填充中的4
是動態的一組數組。