在对象中循环嵌套数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在对象中循环嵌套数组相关的知识,希望对你有一定的参考价值。

我想遍历这个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();

投标总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"
        
    ]    

谢谢,感谢您的帮助。

答案

你是对的,你需要另一个循环,但你可以用.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();
另一答案

一个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));
);

以上是关于在对象中循环嵌套数组的主要内容,如果未能解决你的问题,请参考以下文章

Vue.js 在 v-for 循环中访问嵌套对象

javascript 数组对象与嵌套循环写法

Node.js 循环遍历嵌套的 Javascript 对象

嵌套 JSON 对象中的数组使用 for-in 来循环访问每个数组

Json嵌套数据循环

来自对象键和嵌套数组的 Vue.js v-for 循环