如何访问此嵌套 javascript 对象中的嵌套键?

发布于 2025-01-02 07:24:41 字数 941 浏览 0 评论 0原文

我有以下 javascript 对象:

result = {
    "banking6dig":{
        "GM-B-001":{
            "releaseDate":"2/2/2012 14:44","noOfHex":"3","versInfo":"6 digit Banking"
        },
        "GM-B-002":{
            "releaseDate":"1/2/2012 14:46","noOfHex":"3","versInfo":"6 digit Banking with changes"
        }
    },
    "paynpark":[]
} 

说明:

“banking6dig”和“paynpark”是应用程序
“banking6dig”有两个颠覆:“GM-B-001”和“GM-B-002”
“paynpark”没有颠覆。

此外,每个子版本都有自己的属性,即“releaseDate”、“noOfHex”和“versInfo”。

这个对象“结果”是在 php 请求之后构建的,因此它可以有任意数量的应用程序和子版本;但格式始终相同。

我在 jsFiddle 上尝试过: http://jsfiddle.net/2JLtZ/1/

  1. 我该怎么办找出每个“应用程序”中的“颠覆”数量? (我得到了大约 40 个“颠覆”!)
  2. 如何将应用程序添加到对象“结果”,例如“电力”并重置其属性?
  3. 如何向“paynpark”添加颠覆,例如“fixedRate”?
  4. 如何修改颠覆属性,例如将“GM-B-001”的“releaseDate”更改为“3/12/2012 14:46”?

I have the following javascript object:

result = {
    "banking6dig":{
        "GM-B-001":{
            "releaseDate":"2/2/2012 14:44","noOfHex":"3","versInfo":"6 digit Banking"
        },
        "GM-B-002":{
            "releaseDate":"1/2/2012 14:46","noOfHex":"3","versInfo":"6 digit Banking with changes"
        }
    },
    "paynpark":[]
} 

Explanation:

"banking6dig" and "paynpark" are applications
"banking6dig" has two subversions : "GM-B-001" and "GM-B-002"
"paynpark" has no subversions.

Additionally, each subversion has its own properties,viz, "releaseDate", "noOfHex", and "versInfo".

This object "result" is built after a php request, so it can have any number of apps and subversions; the format, however is ALWAYS the same.

I have tried this on jsFiddle: http://jsfiddle.net/2JLtZ/1/

  1. How do I find out the number of "subversions" in each "app"? (I get some 40 "subversions"!)
  2. How do I add an app to the object "result", e.g., "electricity" with its properties reset?
  3. How do I add a subversion to "paynpark", e.g. "fixedRate"?
  4. How do I modify a subversions properties, for example change "releaseDate" of "GM-B-001" to "3/12/2012 14:46"?

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

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

发布评论

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

评论(2

束缚m 2025-01-09 07:24:41

1.2.3

var i=0;
for (var j in result['banking6dig']) i++;
alert('banking6dig has '+i+' subversions'):

.

result.electricity={}

感谢

paynpark.fixedRate={};

AlienWebguy 指出了这一点。

4.

result['banking6dig']["GM-B-001"]["releaseDate"]="3/12/2012 14:46";

1.

var i=0;
for (var j in result['banking6dig']) i++;
alert('banking6dig has '+i+' subversions'):

2.

result.electricity={}

3.

paynpark.fixedRate={};

Thanks AlienWebguy for pointing this out.

4.

result['banking6dig']["GM-B-001"]["releaseDate"]="3/12/2012 14:46";
奈何桥上唱咆哮 2025-01-09 07:24:41
result = {"banking6dig":{"GM-B-001":{"releaseDate":"2/2/2012 14:44","noOfHex":"3","versInfo":"6 digit Banking"},"GM-B-002":{"releaseDate":"1/2/2012 14:46","noOfHex":"3","versInfo":"6 digit Banking with changes"}},"paynpark":[]};

var app = [],
    svn = {};

for (var _app in result) {
    if(result.hasOwnProperty(_app)){
        app.push(_app);
        svn[_app] = [];
        for (var _svn in result[_app]){
            if(result[_app].hasOwnProperty(_svn)){
                svn[_app].push(_svn);
            } 
        }
    }  
}

// How man apps?
alert(app.length);

// How many subversions?
alert(svn['banking6dig'].length);

// Add an app 'electricity'
result.electricity = {};

// Add subversion to paynpark
result.paynpark.fixedRate = {}

// Add electricity
result.electricity = {};
app.push('electricity');

// Modify subversion
result.banking6dig['GM-B-001'].releaseDate = '3/12/2012';

console.log(result);

演示:http://jsfiddle.net/AlienWebguy/DpCTP/

result = {"banking6dig":{"GM-B-001":{"releaseDate":"2/2/2012 14:44","noOfHex":"3","versInfo":"6 digit Banking"},"GM-B-002":{"releaseDate":"1/2/2012 14:46","noOfHex":"3","versInfo":"6 digit Banking with changes"}},"paynpark":[]};

var app = [],
    svn = {};

for (var _app in result) {
    if(result.hasOwnProperty(_app)){
        app.push(_app);
        svn[_app] = [];
        for (var _svn in result[_app]){
            if(result[_app].hasOwnProperty(_svn)){
                svn[_app].push(_svn);
            } 
        }
    }  
}

// How man apps?
alert(app.length);

// How many subversions?
alert(svn['banking6dig'].length);

// Add an app 'electricity'
result.electricity = {};

// Add subversion to paynpark
result.paynpark.fixedRate = {}

// Add electricity
result.electricity = {};
app.push('electricity');

// Modify subversion
result.banking6dig['GM-B-001'].releaseDate = '3/12/2012';

console.log(result);

Demo: http://jsfiddle.net/AlienWebguy/DpCTP/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文