在 for 循环中创建新对象

发布于 2024-09-19 11:03:52 字数 483 浏览 9 评论 0原文

我想创建一个新对象并为存储在某个 json 中的每个数组分配一些属性。我基本上都在工作,除了......

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();
        }

当我输出对象属性时,一切都是未定义的。

如果我在循环外创建一个对象并为其分配属性,它似乎工作正常,只是第一组属性被以下内容覆盖。不知道为什么我无法在循环内动态创建对象并分配属性。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〆凄凉。 2024-09-26 11:03:52

您实际上从未设置过 a 的任何属性。您只需设置 sup2 的属性。顺便说一句,你还有其他不必要的东西,比如 var Key = key; 试试这个:

for (var i in json) {
    var a = new supplement();
    for (var key in json[i]) {
        a[key] = json[i][key];
    }
    a.outputProperties();
}

You 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();
}
沫雨熙 2024-09-26 11:03:52

您粘贴的代码对我来说看起来不正确,因为它似乎没有结合在一起。

这三行的作用是什么:

     a = 0;
     a++;
     a = new supplement();

你似乎用there做了三件相互矛盾的事情。我的猜测是 a 应该是您未显示的某些外部事物的索引。

那么

     sup2

与您之前所做的supplement() 应该是什么关系呢?

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?

情深已缘浅 2024-09-26 11:03:52

戴夫史密斯的答案非常接近我所需要的,但它没有在循环内创建新对象。这是我更新的代码,它提供了所需的结果:

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();
        }

每个新对象现在都存储在一个数组 theGoods[] 中;
我现在可以通过编写如下内容来引用该对象: theGoods["obj2"].someMethod();

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();

携君以终年 2024-09-26 11:03:52
for (var i in json) {

        a = new supplement();

        for (var key in json[i]) {
            var Value = json[i][key];
            a[Key] = Value;
        }
        a.outputProperties();
    }
for (var i in json) {

        a = new supplement();

        for (var key in json[i]) {
            var Value = json[i][key];
            a[Key] = Value;
        }
        a.outputProperties();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文