admin管理员组文章数量:1316561
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();
}
本文标签: javascriptCreate new object within a forinloopStack Overflow
版权声明:本文标题:javascript - Create new object within a for-in-loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741965832a2407542.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论