admin管理员组文章数量:1289582
so, I have a 2d array, which i decalre like this:
var grille = new Array(60).fill(new Array(30).fill(false));
I want to be able to change the value of one cell in the array, but when i do
grille[x][y] = "new value";
I have all the x array which contains "new value" at the index y, instead of array[x][y].
so i have
grille[1][y] = "new value"
grille[2][y] = "new value"
grille[3][y] = "new value"
instead of
grille[x][y] = "new value"
grille[1][y] = false
for example. It may be a noob error, but i'm very new to javascript and don't know how to do this.
Thanks for your help.
so, I have a 2d array, which i decalre like this:
var grille = new Array(60).fill(new Array(30).fill(false));
I want to be able to change the value of one cell in the array, but when i do
grille[x][y] = "new value";
I have all the x array which contains "new value" at the index y, instead of array[x][y].
so i have
grille[1][y] = "new value"
grille[2][y] = "new value"
grille[3][y] = "new value"
instead of
grille[x][y] = "new value"
grille[1][y] = false
for example. It may be a noob error, but i'm very new to javascript and don't know how to do this.
Thanks for your help.
Share Improve this question asked Mar 8, 2017 at 14:03 JoëlJoël 1052 silver badges10 bronze badges 3- Not clear what the problem/question is. – Waxi Commented Mar 8, 2017 at 14:09
-
I want to do
grille[x][y] = "new value"
, to change the value ofgrille[x][y]
, but when i do this, not onlygrille[x][y]
is changed, but alsogrille[1][y], grille[2][y],...
and i don't know why – Joël Commented Mar 8, 2017 at 14:25 - Not the best person to explain this, but the issue is that all the arrays in grille are not actually unique, they are all sharing the same referenced array, which happens when you create them the way you did. I think you're looking for a 'deep copy' of the array. – Waxi Commented Mar 8, 2017 at 14:36
3 Answers
Reset to default 8In your case 'new Array(30).fill(false)' will be created only once. It wont be created for each element in 'new Array(60)'. So all 60 cells holds the same array reference. So, if you change one - it will update all its reference.
var grille = (new Array(60)).fill().map(function(){ return new Array(30).fill(false);});
Here, for each element in 'new Array(60)' 'new Array(30)' will be created and each cell hold a different reference now. Hope this helps.
So what I was doing with this is that var grille = new Array(60).fill(new Array(30).fill(false));
filled grille with the same new array(30)
and it wasn't creating a new array for each index of grille like I thought.
So What I did instead is this
grille= new Array(60);
for (var i = 0; i < grille.length; i++) {
grille[i] = new Array(30).fill(false);
}
Anyways, since the ES5 (I believe), you can do it this way:
var grille = Array.from(new Array(60),_=>new Array(30).fill(false))
本文标签: Javascriptchange cell value in 2D arrayStack Overflow
版权声明:本文标题:Javascript : change cell value in 2D array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741477957a2380995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论