[英]Looping Through and Printing Nested JSON Object Members
I have successfully returned the object (a store) I was searching for, but now I want to loop through the array of attendants and print the attendants for the store. 我已经成功返回了要搜索的对象(商店),但是现在我想遍历服务员数组并为商店打印服务员。 I cannot use jQuery $each() to iterate. 我不能使用jQuery $ each()进行迭代。 What can I do? 我能做什么? Thank you I appreciate your assistance. 谢谢,谢谢您的协助。
totals.js (my service) totals.js(我的服务)
export const baseUrl = '/src/areas/store-totals/services/tender-total-data.json';
export const all = args =>
// http.post.standard(baseUrl + 'orders/tenderTotals', {}, args);
http.get(baseUrl).
then(function onSuccess(response) {
console.log("The response is :" , response);
}).
catch(function onError(response) {
console.log(response);
});
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",
"date":"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",
"date":"08/21/2018"
}
]
}
If you want to get the attandants for the particular store, you can use array.find as follows, 如果要获取特定商店的服务员,可以使用array.find,如下所示,
let 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", "date": "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", "date": "08/21/2018" } ] }; let attendants = data.stores.find(store=>store.storeId == 3047).attendants; attendants.forEach( (element) => { console.log(element); });
To iterate the array elements you can use functions such as map or forEach. 要迭代数组元素,可以使用诸如map或forEach之类的函数。 If you use map, the iterating logic would be something similar to : 如果使用map,则迭代逻辑将类似于:
attendants.map((item) => {
console.log(item);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.