0

我正在尝试使用 for..in 循环来访问“事件”中的描述键/值,但目前我不完全确定如何实现这一点。首先,我使用了 for..in 并将其注销,这将返回响应中的所有顶级条目,我现在如何向下钻取并选择 Event.description?我首先认为它是 data[prop].Event.description 但事实并非如此。我应该使用普通的 for 循环,然后在其中使用 for..in 吗?

这是我当前的代码:

$(document).ready(function() {

    var data = {
        "status": "ok",
        "code": "200",
        "message": "event details",
        "data": [{
            "Event": {
                "id": "1",
                "name": "Sample Event Number 1",
                "description": "Sample Event Number 4 Description ....",
                "event_date": "2012-05-31 00:00:00",
                "Band": [{
                    "id": "1",
                    "name": "Support #1",
                    "BandsEvent": {
                        "id": "7",
                        "band_id": "2",
                        "event_id": "8",
                        "created": "2012-05-23 15:53:56",
                        "modified": "2012-05-23 15:53:56"
                    }},
                {
                    "id": "2",
                    "name": "Support #2",
                    "BandsEvent": {
                        "id": "8",
                        "band_id": "1",
                        "event_id": "8",
                        "created": "2012-05-23 15:53:57",
                        "modified": "2012-05-23 15:53:57"
                    }}]
            }},
        {
            "Event": {
                "id": "2",
                "name": "Sample Event Number 2",
                "description": "Sample Event Number 4 Description ....",
                "event_date": "2012-05-31 00:00:00",
                "Band": [{
                    "id": "2",
                    "name": "Another Crazy Band",
                    "BandsEvent": {
                        "id": "3",
                        "band_id": "2",
                        "event_id": "8",
                        "created": "2012-05-23 15:53:56",
                        "modified": "2012-05-23 15:53:56"
                    }},
                {
                    "id": "4",
                    "name": "The Band",
                    "BandsEvent": {
                        "id": "8",
                        "band_id": "1",
                        "event_id": "8",
                        "created": "2012-05-23 15:53:57",
                        "modified": "2012-05-23 15:53:57"
                    }}]

            }}]
    }


    var prop;

    for (prop in data) {
        console.log( data[prop] );
        // data.Event.description 
    }

});​
4

1 回答 1

1

这应该做你想要的:

for (var i = 0; i < data.data.length; i++) {
    console.log(data.data[i].Event.description);        
}

我应该补充一点,您的代码不起作用的原因是“ prop”变量首先是“ status”,然后是“”,然后是“ code”,然后是messagedata”。Status/code/message 没有“Event”属性,因此如果您尝试访问,您的代码将返回 undefined data[prop].Event。这里我们特意挑出来。而且由于 data.data 是一个数组,所以没有理由使用for .. in循环,而只是一个常规的 for 循环。

同样,如果您想打印出描述和波段,您可以执行以下操作:

for (var i = 0; i < data.data.length; i++) {
    console.log(data.data[i].Event.description + " has the following bands:"); 
    for (var j = 0; j < data.data[i].Event.Band.length; j++) {  
        console.log(data.data[i].Event.Band[j].name); 
    }       
}
于 2012-06-07T08:50:48.387 回答