I want to create a new object and assign some properties for each array stored within some json. I have this mostly working except...
for (var i in json) {
a = 0;
a++;
a = new Object();
for (var key in json[i]) {
var Key = key;
var Value = json[i][key];
a[Key] = Value;
}
a.outputProperties();
}
When I output the object properties, everything is undefined.
If I create a single object outside the loop and assign the properties to it, it seems to work OK except that the first set of properties get overwritten with the following. Not sure why I wouldn't be able to create objects and assign properties inside the loop dynamically.
I want to create a new object and assign some properties for each array stored within some json. I have this mostly working except...
for (var i in json) {
a = 0;
a++;
a = new Object();
for (var key in json[i]) {
var Key = key;
var Value = json[i][key];
a[Key] = Value;
}
a.outputProperties();
}
When I output the object properties, everything is undefined.
If I create a single object outside the loop and assign the properties to it, it seems to work OK except that the first set of properties get overwritten with the following. Not sure why I wouldn't be able to create objects and assign properties inside the loop dynamically.
Share Improve this question edited Sep 14, 2010 at 18:03 chromaloop asked Sep 14, 2010 at 17:13 chromaloopchromaloop 2225 silver badges11 bronze badges 4-
You should be able to. We may need to see more code (for example, where does
sup2
e from?). – palswim Commented Sep 14, 2010 at 17:21 - 1 a = 0; a++; a = new supplement(); In each iteration you are setting a to 0, then 1, then to an object. What's up with that? – Ronald Commented Sep 14, 2010 at 17:21
- I wanted to create a new object for each loop iteration. I thought I could increment a variable value and use that to create the new object name. That didn't work. – chromaloop Commented Sep 14, 2010 at 21:14
- This did the trick though: theGoods["obj"+i] = new Object(); – chromaloop Commented Sep 14, 2010 at 21:15
4 Answers
Reset to default 3You never actually set any properties of a
. You just set properties of sup2
. On a side note you have other unnecessary stuff in there like var Key = key;
Try this:
for (var i in json) {
var a = new supplement();
for (var key in json[i]) {
a[key] = json[i][key];
}
a.outputProperties();
}
The code you pasted doesn't look right to me, in the sense of it doesn't seem to hang together.
What do these three lines do:
a = 0;
a++;
a = new supplement();
You seem to do three contradictory things with a there. My guess is that a's meant to be an index to some external thing you don't show.
Then what is
sup2
supposed to be, some relationship to the supplement() you made earlier?
Dave Smith's answer was pretty close to what I needed but it didn't create new objects within the loop. Here's my updated code that provided the desired result:
for (var i in json) {
theGoods["obj"+i] = new Object();
for (var key in json[i]) {
theGoods["obj"+i][key] = json[i][key];
}
theGoods["obj"+i].outputProperties();
}
Each new object is now stored within an array, theGoods[];
I can now reference that object by writing something like: theGoods["obj2"].someMethod();
for (var i in json) {
a = new supplement();
for (var key in json[i]) {
var Value = json[i][key];
a[Key] = Value;
}
a.outputProperties();
}