簡體   English   中英

循環遍歷和打印嵌套的JSON對象成員

[英]Looping Through and Printing Nested JSON Object Members

我已經成功返回了要搜索的對象(商店),但是現在我想遍歷服務員數組並為商店打印服務員。 我不能使用jQuery $ each()進行迭代。 我能做什么? 謝謝,謝謝您的協助。

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);
  });

投標總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"
        }
    ]    
}

如果要獲取特定商店的服務員,可以使用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); }); 

要迭代數組元素,可以使用諸如map或forEach之類的函數。 如果使用map,則迭代邏輯將類似於:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM