[英]Loop Through Nested Array in object
I want to loop through this json file, and print the attendantName
for each store. 我想通过这个JSON文件回路,并打印
attendantName
每个商店。 I am able to print the key and value, and I am able to print the first attendant in the arrays of attendants. 我能够打印键和值,并且能够打印第一组话务员。 But I need to print all the attendants in the array.
但是我需要打印阵列中的所有服务员。 I think I need a nested loop.
我想我需要一个嵌套循环。 How can I do this?
我怎样才能做到这一点? This is what I have so far.
到目前为止,这就是我所拥有的。
script.js 的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 投标总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"
}
]
}
Thanks I appreciate your assistance. 谢谢,谢谢您的协助。
You are right, you need another loop, but you can make it a little easier with .forEach
: 没错,您需要另一个循环,但是您可以使用
.forEach
使其变得更简单:
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);
data.stores.forEach(function(key, val){
console.log("The key is: ", key, "The value is; ", val);
val.attendants.forEach(a => console.log("Attendant Name: ",a.attendantName));
console.log(val.storeId);
})
}
};
request.send();
One forEach
to iterate stores
and the second inside to iterate attendants
其中
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)); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.