遍历对象中的嵌套数组

时间:2018-08-29 17:56:00

标签: javascript jquery arrays json loops

我想遍历此json文件,并为每个商店打印attendantName。我能够打印键和值,并且能够打印第一组话务员。但是我需要打印阵列中的所有服务员。我想我需要一个嵌套循环。我怎样才能做到这一点?到目前为止,这就是我所拥有的。

script.js

var data;
var request = new XMLHttpRequest();

request.open('GET', 'js/tender-total-data.json');

request.onreadystatechange = function () {
  if (request.status === 200 && request.readyState === 4) {
    data = JSON.parse(request.responseText);
    $.each(data.stores, function(key, val){
      console.log("The key is: ", key, "The value is; ", val);
      console.log("Attendant Name: ", (val.attendants[0].attendantName));
      console.log(val.storeId);
    })

  }
};

request.send();

tender-total-data.json

{
  "stores": [
        {
        "storeName": "Master Bistro",
        "storeId": "3046",
        "attendants": [
            {
            "attendantName": "Janis Joplin",
            "attendantId": "9784526",
            "total": 2000,
            "tenderTotal": {
                "Cash": 500,
                "TC": 0,
                "UOD": 500,
                "MC": 250,
                "VI": 250,
                "AX": 250,
                "DI": 250,
                "JC": 0,
                "DC": 0,
                "UOP": 0,
                "GN": 0,
                "UOGC": 0,
                "HOTEL": 0,
                "NCTNCG": 0
                }
            },
            {
            "attendantName": "David Bowie",
            "attendantId": "2589456",
            "total": 14675,
            "tenderTotal": {
                "Cash": 175,
                "TC": 0,
                "UOD": 100,
                "MC": 9500,
                "VI": 3500,
                "AX": 550,
                "DI": 850,
                "JC": 0,
                "DC": 0,
                "UOP": 0,
                "GN": 0,
                "UOGC": 0,
                "HOTEL": 0,
                "NCTNCG": 0
                }
            },
            {
            "attendantName": "Michael Jackson",
            "attendantId": "5478264",
            "total": 15599,
                "tenderTotal": {
                    "Cash": 250,
                    "TC": 0,
                    "UOD": 80,
                    "MC": 5624,
                    "VI": 6895,
                    "AX": 2500,
                    "DI": 250,
                    "JC": 0,
                    "DC": 0,
                    "UOP": 0,
                    "GN": 0,
                    "UOGC": 0,
                    "HOTEL": 0,
                    "NCTNCG": 0
                }
            }
        ],
            "message": "Store totals for 08/20/2018"
    },{

        "storeName": "The Master  Marketplace",
        "storeId": "3047",
        "attendants": [
            {
                "attendantName": "Dirk Novitski",
                "attendantId": "9784527",
                "total": 2000,
                "tenderTotal": {
                    "Cash": 500,
                    "TC": 0,
                    "UOD": 500,
                    "MC": 250,
                    "VI": 250,
                    "AX": 250,
                    "DI": 250,
                    "JC": 0,
                    "DC": 0,
                    "UOP": 0,
                    "GN": 0,
                    "UOGC": 0,
                    "HOTEL": 0,
                    "NCTNCG": 0
                }
            },
            {
                "attendantName": "Carmello Anthony",
                "attendantId": "2589458",
                "total": 14675,
                "tenderTotal": {
                    "Cash": 175,
                    "TC": 0,
                    "UOD": 100,
                    "MC": 9500,
                    "VI": 3500,
                    "AX": 550,
                    "DI": 850,
                    "JC": 0,
                    "DC": 0,
                    "UOP": 0,
                    "GN": 0,
                    "UOGC": 0,
                    "HOTEL": 0,
                    "NCTNCG": 0
                }
            },
            {
                "attendantName": "Stevie Wonder",
                "attendantId": "5478266",
                "total": 15599,
                "tenderTotal": {
                    "Cash": 250,
                    "TC": 0,
                    "UOD": 80,
                    "MC": 5624,
                    "VI": 6895,
                    "AX": 2500,
                    "DI": 250,
                    "JC": 0,
                    "DC": 0,
                    "UOP": 0,
                    "GN": 0,
                    "UOGC": 0,
                    "HOTEL": 0,
                    "NCTNCG": 0
                }

            }
        ],
            "message": "Store totals for 08/22/2018"
        }
    ]    
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您是对的,您需要另一个循环,但是可以使用_55: d:{results: Array(291)} 使循环更简单:

.forEach

答案 1 :(得分:0)

一个forEach用于迭代stores,第二个attendants用于迭代var data = { stores: [ { storeName: "Master Bistro", storeId: "3046", attendants: [ { attendantName: "Janis Joplin", attendantId: "9784526", total: 2000, tenderTotal: { Cash: 500, TC: 0, UOD: 500, MC: 250, VI: 250, AX: 250, DI: 250, JC: 0, DC: 0, UOP: 0, GN: 0, UOGC: 0, HOTEL: 0, NCTNCG: 0 } }, { attendantName: "David Bowie", attendantId: "2589456", total: 14675, tenderTotal: { Cash: 175, TC: 0, UOD: 100, MC: 9500, VI: 3500, AX: 550, DI: 850, JC: 0, DC: 0, UOP: 0, GN: 0, UOGC: 0, HOTEL: 0, NCTNCG: 0 } }, { attendantName: "Michael Jackson", attendantId: "5478264", total: 15599, tenderTotal: { Cash: 250, TC: 0, UOD: 80, MC: 5624, VI: 6895, AX: 2500, DI: 250, JC: 0, DC: 0, UOP: 0, GN: 0, UOGC: 0, HOTEL: 0, NCTNCG: 0 } } ], message: "Store totals for 08/20/2018" }, { storeName: "The Master Marketplace", storeId: "3047", attendants: [ { attendantName: "Dirk Novitski", attendantId: "9784527", total: 2000, tenderTotal: { Cash: 500, TC: 0, UOD: 500, MC: 250, VI: 250, AX: 250, DI: 250, JC: 0, DC: 0, UOP: 0, GN: 0, UOGC: 0, HOTEL: 0, NCTNCG: 0 } }, { attendantName: "Carmello Anthony", attendantId: "2589458", total: 14675, tenderTotal: { Cash: 175, TC: 0, UOD: 100, MC: 9500, VI: 3500, AX: 550, DI: 850, JC: 0, DC: 0, UOP: 0, GN: 0, UOGC: 0, HOTEL: 0, NCTNCG: 0 } }, { attendantName: "Stevie Wonder", attendantId: "5478266", total: 15599, tenderTotal: { Cash: 250, TC: 0, UOD: 80, MC: 5624, VI: 6895, AX: 2500, DI: 250, JC: 0, DC: 0, UOP: 0, GN: 0, UOGC: 0, HOTEL: 0, NCTNCG: 0 } } ], message: "Store totals for 08/22/2018" } ] }; data.stores.forEach(o => { o.attendants.forEach(n => console.log(n.attendantName)); });

                Learn More

        Hide [x]








                            Colors



                    Fetching Colors description...




            Show more topics







                            Art exhibitions



                    Fetching Art exhibitions description...








                            Abstract art



                    Fetching Abstract art description...








                            Representational art



                    Fetching Representational art description...