If we create some objects, and fill an array with those objects, are the names stored within the array, or only the properties of the object? I guess this may be trivial, but I haven't been able to find an answer.
var boxA = {color: "red", width: 100};
var boxB = {color: "yellow", width: 200};
var boxC = {color: "blue", width: 300};
boxArray = [boxA, boxB, boxC];
for (var i = 0; i < boxArray.length; i++) {
//****
// What code do we insert here to log
// boxA
// boxB
// boxC
//****
}
Of course, it is a trivial workaround to add
boxA.box = boxA;
etc and then call
console.log(boxArray[i].box);
But is that really necessary?
{ boxA: {color: 'red', width: 100}, boxB: {color: 'yellow', width: 200} }
and then iterate the keys of the nested object. – ray hatfield May 27 '15 at 22:42@Ben Philipp
that doesn't mater, it's an Array of Objects. Only primitive variables don't get affected when you are accessing Array Elements, so you are affecting the original Object if you make changes to Array variable properties or those Array variables.console.log(boxArray[i])
should work in your test Browser. You may want to useObject.create()
and its backward compatible counterpart, to create anew
instance of your Objects, if you don't want to affect the original Object. – StackSlave May 27 '15 at 22:59