简体   繁体   中英

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. What can I do? Thank you I appreciate your assistance.

totals.js (my service)

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

{
  "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,

 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. If you use map, the iterating logic would be something similar to :

attendants.map((item) => {
 console.log(item);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM