初始化“复杂”嵌套的 JavaScript/对象

发布于 2024-12-16 20:13:59 字数 374 浏览 0 评论 0原文

我正在制作一个生成 Javascript 文件的程序。我从未使用过 Javascript,所以我可能会问一个愚蠢的问题:) 无论如何,这个 Javascript 文件是用数据初始化的。

这是一个示例行,我使用一个对象初始化数组中的一个条目,该对象包含两个属性,其中一个属性是一个包含 3 个对象的数组...

或者,好吧,至少是这个想法...无论如何我可以这样做让“M”成为一个可以正常索引的真正数组吗?

W1[242] = {"W":"authors","M":{ "ArrItem0":{"U":32,"S":4.37},"ArrItem1":{"U":38,"S":4.02},"ArrItem2":{"U":406,"S":1.53} } };

I am making a program that generates a Javascript file. I have never worked with Javascript, so I may be asking a stupid question :) Anyhow, this Javascript files gets initialized with data.

Here is an example line where I initialize an entry in an array with an object that contains two properties whereof one of them is an array containing 3 objects...

Or, well, that was the idea at least... Is there anyway I can do so so "M" becomes a real array that can be indexed normally?

W1[242] = {"W":"authors","M":{ "ArrItem0":{"U":32,"S":4.37},"ArrItem1":{"U":38,"S":4.02},"ArrItem2":{"U":406,"S":1.53} } };

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

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

发布评论

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

评论(3

一袭白衣梦中忆 2024-12-23 20:13:59
W1[242] = {"W":"authors","M":[ {"U":32,"S":4.37},{"U":38,"S":4.02},{"U":406,"S":1.53} ] };
W1[242] = {"W":"authors","M":[ {"U":32,"S":4.37},{"U":38,"S":4.02},{"U":406,"S":1.53} ] };
飘落散花 2024-12-23 20:13:59

数组表示法如下,带有括号 []

W1[242] = 
    { 
       "W": "authors",
       "M": [{"U":32,"S":4.37}, {"U":38,"S":4.02},{"U":406,"S":1.53}]
    };

The array notation is the following, with brackets []:

W1[242] = 
    { 
       "W": "authors",
       "M": [{"U":32,"S":4.37}, {"U":38,"S":4.02},{"U":406,"S":1.53}]
    };
意中人 2024-12-23 20:13:59

取决于您想要如何使用 M 中的内容。这是一个选项,其中 M 的每个元素都是它自己的对象。

W1[242] = {"W":"authors","M":[ {"U":32,"S":4.37},{"U":38,"S":4.02},{"U":406,"S":1.53} ] };

Depends on how you want to work with what is in M. Here is one option where each element of M is it's own object.

W1[242] = {"W":"authors","M":[ {"U":32,"S":4.37},{"U":38,"S":4.02},{"U":406,"S":1.53} ] };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文