如何迭代由json对象中的键访问的数组[重复]

Posted

技术标签:

【中文标题】如何迭代由json对象中的键访问的数组[重复]【英文标题】:How can I iterate over an array that is accesed by a key in a json Object [duplicate] 【发布时间】:2016-05-03 03:32:14 【问题描述】:

这是我的字符串化 JSON 对象:


    "bookingsList": [
        "bookingID": 5,
        "destination": "BSO",
        "flightDate": "2015-12-07T00:00:00",
        "flightScheduleId": 113,
        "origin": "MNL",
        "passengerList": [
            "availedInsurance": true,
            "availedMeal": false,
            "birthDate": null,
            "carryOnBaggage": 5.1,
            "checkInBaggage": 4.5,
            "columnId": 1,
            "firstname": "Peter",
            "gender": "M",
            "lastname": "North",
            "middlename": "West",
            "nationality": null,
            "rowId": "A"
        ]
    , 
        "bookingID": 6,
        "destination": "BSO",
        "flightDate": "2015-12-07T00:00:00",
        "flightScheduleId": 113,
        "origin": "MNL",
        "passengerList": [
            "availedInsurance": false,
            "availedMeal": false,
            "birthDate": null,
            "carryOnBaggage": 4.2,
            "checkInBaggage": 3.4,
            "columnId": 2,
            "firstname": "Mark Justin",
            "gender": "M",
            "lastname": "Jalandoni",
            "middlename": "Manzano",
            "nationality": null,
            "rowId": "A"
        ]
    ]

那么我该如何迭代 bookingsList 中的对象呢?我将在桌子上放置一些属性。

编辑:原来我使用的 for in 循环没有迭代,因为我忘记在可迭代对象变量之前放置一个“var”。

【问题讨论】:

bookingsList 只是一个数组,所以任何循环结构都可以,例如一个普通的旧 for 循环。 你知道循环的概念吗??? 【参考方案1】:

一个简单的for循环就足够了:

var jsonObj = 
    "bookingsList": [
        "bookingID": 5,
        "destination": "BSO",
        "flightDate": "2015-12-07T00:00:00",
        "flightScheduleId": 113,
        "origin": "MNL",
        "passengerList": [
            "availedInsurance": true,
            "availedMeal": false,
            "birthDate": null,
            "carryOnBaggage": 5.1,
            "checkInBaggage": 4.5,
            "columnId": 1,
            "firstname": "Peter",
            "gender": "M",
            "lastname": "North",
            "middlename": "West",
            "nationality": null,
            "rowId": "A"
        ]
    , 
        "bookingID": 6,
        "destination": "BSO",
        "flightDate": "2015-12-07T00:00:00",
        "flightScheduleId": 113,
        "origin": "MNL",
        "passengerList": [
            "availedInsurance": false,
            "availedMeal": false,
            "birthDate": null,
            "carryOnBaggage": 4.2,
            "checkInBaggage": 3.4,
            "columnId": 2,
            "firstname": "Mark Justin",
            "gender": "M",
            "lastname": "Jalandoni",
            "middlename": "Manzano",
            "nationality": null,
            "rowId": "A"
        ]
    ]


// For easy reference
var bookingsList = jsonObj.bookingsList;

for (i = 0; i < bookingsList.length; i++) 
    var booking = bookingsList[i]; // Grab booking data
    console.log(booking.bookingID); // Log booking data

【讨论】:

【参考方案2】:
$.each(json.bookingsList, function(index, prop)
     console.log(prop);
);

【讨论】:

纯代码答案对于帮助 OP 了解正在发生的事情并没有真正有用。也许给你的答案一个简短的解释。

以上是关于如何迭代由json对象中的键访问的数组[重复]的主要内容,如果未能解决你的问题,请参考以下文章

JS去除数组中重复的数字

ng-repeat:访问对象数组中每个对象的键和值

如何迭代嵌套的 JSON 对象 [重复]

JS 遍历对象数组并将值更改为匹配的键并创建一个包含对象数组 [0] 中的键的新对象

如何通过值获取 JavaScript 对象中的键?

Vue Js 中的 Key 和 value 如何迭代