javascript或jquery:循环一个多维对象 [英] javascript or jquery: Looping a multidimensional object

查看:82
本文介绍了javascript或jquery:循环一个多维对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用JSON,我创建了这个示例。

I just started playing around with JSON and I have created this example.

var shows = {

    "ShowA": 
                {   "Date"      : "November 3-5, 2011",
                    "Phone"     : "111-111-1111",
                    "Location"  : "some location",
                    "url"       : "http://www.showA.com"
                },

    "ShowB": 
                {   "Date"      : "January 15-18, 2012",
                    "Phone"     : "222-222-2222",
                    "Location"  : "another location",
                    "url"       : "http://www.showB.com"
                }

};

我想出了如何访问每一位信息......即:alert(shows.ShowA。日期);

I figured out how to access each bit of information...ie: alert(shows.ShowA.Date);

但是,我无法弄清楚如何循环整个节目对象,以便提醒每个节目和每个节目的属性。我需要将其更改为数组吗?

However, I can't figure out how to loop the entire shows object in order alert each show and each show's properties. Do I need to change it to an array?

任何帮助都将不胜感激。

Any help would be greatly appreciated.

推荐答案

您可以使用 ...循环播放

you can use a for ... in loop:

for(var key in shows) {
    if (shows.hasOwnProperty(key)) {
        alert(shows[key].Date);
    }
}

重要的是要注意对象没有排序顺序,但数组呢。因此,如果您想按日期排序,则需要使用数组。

It's important to note that an object has no sort order, but an array does. So if you wanted to sort by dates, you would need to use an array.

此外,最好使用 Object.hasOwnProperty

这篇关于javascript或jquery:循环一个多维对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆