admin管理员组文章数量:1425880
var initGrid = function() {
//creating a grid while initialising it
var grid = [];
// declaring each grid element as a Object having three properties
var gridElement = {
x: Number,
y: Number,
val: Number
};
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
gridElement.x = i;
gridElement.y = j;
gridElement.val = 0;
grid.push(gridElement);
};
};
return grid;
};
console.log(initGrid());
var initGrid = function() {
//creating a grid while initialising it
var grid = [];
// declaring each grid element as a Object having three properties
var gridElement = {
x: Number,
y: Number,
val: Number
};
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
gridElement.x = i;
gridElement.y = j;
gridElement.val = 0;
grid.push(gridElement);
};
};
return grid;
};
console.log(initGrid());
when i run this code on console, all Objects of grid Array are having same values for x and y which are 9 and 9....
But i want to create objects having different values acc to loop variables
Share Improve this question edited Feb 13, 2016 at 10:16 Rajesh 25k5 gold badges50 silver badges83 bronze badges asked Feb 13, 2016 at 10:13 ankit guptaankit gupta 91 gold badge1 silver badge7 bronze badges3 Answers
Reset to default 1This is because, Object are passed by reference. You will have to create different object in every iteration.
Following is the sample.
var initGrid = function() {
//creating a grid while initialising it
var grid = [];
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
var gridElement = {}
gridElement.x = i;
gridElement.y = j;
gridElement.val = 0;
grid.push(gridElement);
};
};
return grid;
};
console.log(initGrid());
You're not declaring a gridElement type that way; Javascript doesn't do that, Typescript would (but that's beside the point).
You see the same values because you only ever have one gridElement object, the one you created with
var gridElement = {
x: Number,
y: Number,
val: Number
};
which, for context, initializes an object with properties x
, y
, and val
all pointing to the Number
constructor function.
Every loop iteration is mutating this same object, and pushing a reference to that one object onto your grid, so you don't actually have 100 grid elements with x
and y
equal to 9
- you have 100 references to the same object, where the last values you wrote to x
and y
were both 9
.
As an example, you can create new objects per loop as follows:
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
var newGridElement = {
x: i,
y: j,
val: 0
};
grid.push(newGridElement);
console.log(newGridElement);
};
};
You can declare a GridElement
constructor function and type of your own, but if you really need that, you'll be doing something like defining a Javascript class of your own.
You can use a user-defined object, with a function and an instance with new
operator [MDN]. This gives an interface for the data structure.
var initGrid = function () {
var grid = [];
function GridElement(x, y, val) { // user-defined function
this.x = x; // properties
this.y = y;
this.val = val;
};
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
grid.push(new GridElement(i, j, 0)); // use an instance of GridElement with new
};
};
return grid;
};
document.write('<pre>' + JSON.stringify(initGrid(), 0, 4) + '</pre>');
本文标签: javascriptpassing Object into ArrayStack Overflow
版权声明:本文标题:javascript - passing Object into Array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745386356a2656391.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论