admin管理员组文章数量:1026989
In my function, I have defined two arrays, the first (array1), has a pre-initialized length. I added the second array (array2) just for testing because I thought the first was behaving strangely.
My code:
function test(n = 3) {
array1 = new Array(n).fill(new Array(n));
array2 = [
[undefined, undefined, undefined],
[undefined, undefined, undefined],
[undefined, undefined, undefined]
];
document.getElementById("output").innerHTML = JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><hr/>";
for (i = 0; i < n; i++) {
array1[i][0] = i;
array2[i][0] = i;
}
document.getElementById("output").innerHTML += JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><hr/>";
}
<button onclick="test();">Press to test</button>
<br/><br/>
<div id="output"></div>
In my function, I have defined two arrays, the first (array1), has a pre-initialized length. I added the second array (array2) just for testing because I thought the first was behaving strangely.
My code:
function test(n = 3) {
array1 = new Array(n).fill(new Array(n));
array2 = [
[undefined, undefined, undefined],
[undefined, undefined, undefined],
[undefined, undefined, undefined]
];
document.getElementById("output").innerHTML = JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><hr/>";
for (i = 0; i < n; i++) {
array1[i][0] = i;
array2[i][0] = i;
}
document.getElementById("output").innerHTML += JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><hr/>";
}
<button onclick="test();">Press to test</button>
<br/><br/>
<div id="output"></div>
In the for
loop, I try to change the first value of the second dimensions. It should output [[0, undefined, undefined], [1, undefined, undefined], [2, undefined, undefined]]
, like the second array does.
My questions are: why does this happen? And, how can I make a pre-initialized array with length n in both dimensions, that behaves like the second array?
Share Improve this question edited Aug 20, 2017 at 14:36 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Aug 20, 2017 at 14:29 SachaSacha 89310 silver badges29 bronze badges 1- Sharing an article, why not to use new Array(), hope this helps -> coderwall./p/h4xm0w/why-never-use-new-array-in-javascript – mindaJalaj Commented Aug 20, 2017 at 14:38
2 Answers
Reset to default 9That's because in case of array1
it contains three arrays, but all three of them point to the same reference variable, that was evaluated when new Array(n)
was executed:
var array1 = new Array(n).fill(new Array(n));
So when the for
loop runs over array1
it is setting the value of the same array reference, while in case of array2
those three arrays are different reference variables.
Here's a slightly modified version of your snippet. Notice the entries into console when the value of array1
's element is being changed. In case of array1
all three child arrays are changing, while in case of array2
the array referenced under the loop using index i
is the only one that changes.
function test(n = 3) {
array1 = new Array(n).fill(new Array(n));
array2 = [
[undefined, undefined, undefined],
[undefined, undefined, undefined],
[undefined, undefined, undefined]
];
document.getElementById("output").innerHTML = JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><i>The mas with nothing in between mean undefined.</i><hr/>";
for (i = 0; i < n; i++) {
array1[i][0] = i;
array2[i][0] = i;
console.log("Array 1: " + JSON.stringify(array1));
console.log("Array 2: " + JSON.stringify(array2));
}
document.getElementById("output").innerHTML += JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><i>The mas with nothing in between mean undefined.</i><hr/>";
}
<button onclick="test();">Press to test</button>
<br/><br/>
<div id="output"></div>
Because Array.fill
The
fill()
method fills all the elements of an array from a start index to an end index with a static value.
takes a static value and fills the array with it. Therefore you get in every element of array1
the same array of the filling.
function test(n = 3) {
var array1 = new Array(n).fill(new Array(n)),
array2 = [[undefined, undefined, undefined], [undefined, undefined, undefined], [undefined, undefined, undefined]],
i;
for (i = 0; i < n; i++) {
array1[i][0] = i;
array2[i][0] = i;
}
document.getElementById("output").innerHTML = array1 + " (array 1) <br/>" + array2 + " (array 2)<br/><br/><i>The mas with nothing in between mean undefined.</i>";
console.log(array1);
console.log(array2);
}
<button onclick="test();">Press to test</button><br/><br/>
<div id="output"></div>
To get an independent filled array, you could use Array.from
and map a new array with mapped values.
var array = Array.from({ length: 3 }, _ => Array.from({ length: 3 }, _ => 4));
array[0][0] = 0;
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
In my function, I have defined two arrays, the first (array1), has a pre-initialized length. I added the second array (array2) just for testing because I thought the first was behaving strangely.
My code:
function test(n = 3) {
array1 = new Array(n).fill(new Array(n));
array2 = [
[undefined, undefined, undefined],
[undefined, undefined, undefined],
[undefined, undefined, undefined]
];
document.getElementById("output").innerHTML = JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><hr/>";
for (i = 0; i < n; i++) {
array1[i][0] = i;
array2[i][0] = i;
}
document.getElementById("output").innerHTML += JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><hr/>";
}
<button onclick="test();">Press to test</button>
<br/><br/>
<div id="output"></div>
In my function, I have defined two arrays, the first (array1), has a pre-initialized length. I added the second array (array2) just for testing because I thought the first was behaving strangely.
My code:
function test(n = 3) {
array1 = new Array(n).fill(new Array(n));
array2 = [
[undefined, undefined, undefined],
[undefined, undefined, undefined],
[undefined, undefined, undefined]
];
document.getElementById("output").innerHTML = JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><hr/>";
for (i = 0; i < n; i++) {
array1[i][0] = i;
array2[i][0] = i;
}
document.getElementById("output").innerHTML += JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><hr/>";
}
<button onclick="test();">Press to test</button>
<br/><br/>
<div id="output"></div>
In the for
loop, I try to change the first value of the second dimensions. It should output [[0, undefined, undefined], [1, undefined, undefined], [2, undefined, undefined]]
, like the second array does.
My questions are: why does this happen? And, how can I make a pre-initialized array with length n in both dimensions, that behaves like the second array?
Share Improve this question edited Aug 20, 2017 at 14:36 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Aug 20, 2017 at 14:29 SachaSacha 89310 silver badges29 bronze badges 1- Sharing an article, why not to use new Array(), hope this helps -> coderwall./p/h4xm0w/why-never-use-new-array-in-javascript – mindaJalaj Commented Aug 20, 2017 at 14:38
2 Answers
Reset to default 9That's because in case of array1
it contains three arrays, but all three of them point to the same reference variable, that was evaluated when new Array(n)
was executed:
var array1 = new Array(n).fill(new Array(n));
So when the for
loop runs over array1
it is setting the value of the same array reference, while in case of array2
those three arrays are different reference variables.
Here's a slightly modified version of your snippet. Notice the entries into console when the value of array1
's element is being changed. In case of array1
all three child arrays are changing, while in case of array2
the array referenced under the loop using index i
is the only one that changes.
function test(n = 3) {
array1 = new Array(n).fill(new Array(n));
array2 = [
[undefined, undefined, undefined],
[undefined, undefined, undefined],
[undefined, undefined, undefined]
];
document.getElementById("output").innerHTML = JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><i>The mas with nothing in between mean undefined.</i><hr/>";
for (i = 0; i < n; i++) {
array1[i][0] = i;
array2[i][0] = i;
console.log("Array 1: " + JSON.stringify(array1));
console.log("Array 2: " + JSON.stringify(array2));
}
document.getElementById("output").innerHTML += JSON.stringify(array1) + " (array 1) <br/>" + JSON.stringify(array2) + " (array 2)<br/><br/><i>The mas with nothing in between mean undefined.</i><hr/>";
}
<button onclick="test();">Press to test</button>
<br/><br/>
<div id="output"></div>
Because Array.fill
The
fill()
method fills all the elements of an array from a start index to an end index with a static value.
takes a static value and fills the array with it. Therefore you get in every element of array1
the same array of the filling.
function test(n = 3) {
var array1 = new Array(n).fill(new Array(n)),
array2 = [[undefined, undefined, undefined], [undefined, undefined, undefined], [undefined, undefined, undefined]],
i;
for (i = 0; i < n; i++) {
array1[i][0] = i;
array2[i][0] = i;
}
document.getElementById("output").innerHTML = array1 + " (array 1) <br/>" + array2 + " (array 2)<br/><br/><i>The mas with nothing in between mean undefined.</i>";
console.log(array1);
console.log(array2);
}
<button onclick="test();">Press to test</button><br/><br/>
<div id="output"></div>
To get an independent filled array, you could use Array.from
and map a new array with mapped values.
var array = Array.from({ length: 3 }, _ => Array.from({ length: 3 }, _ => 4));
array[0][0] = 0;
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
本文标签: Why do these two javascript 2darrays behave differentlyStack Overflow
版权声明:本文标题:Why do these two javascript 2d-arrays behave differently? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745671437a2162480.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论