簡體   English   中英

jQuery jSon獲取具有多個值的子數組

[英]jQuery jSon to get sub array with multiple value

我有從PHP Curl jSon獲取的數據。

以下是jSon數據的示例。

數據如下:

{
"rajaongkir": {
    "query": {
      "origin": "23",
      "destination": "152",
      "weight": 1500,
      "courier": "all"
    },
    "status": {
      "code": 200,
      "description": "OK"
    },
    "origin_details": {
      "city_id": "23",
      "province_id": "9",
      "province": "Jawa Barat",
      "type": "Kota",
      "city_name": "Bandung",
      "postal_code": "40000"
    },
    "destination_details": {
      "city_id": "152",
      "province_id": "6",
      "province": "DKI Jakarta",
      "type": "Kota",
      "city_name": "Jakarta Pusat",
      "postal_code": "10000"
    },
    "results": [
      {
        "code": "pos",
        "name": "POS Indonesia (POS)",
        "costs": [
          {
            "service": "Surat Kilat Khusus",
            "description": "Surat Kilat Khusus",
            "cost": [
              {
                "value": 16500,
                "etd": "2-4",
                "note": ""
              }
            ]
          },
          {
            "service": "Express Next Day",
            "description": "Express Next Day",
            "cost": [
              {
                "value": 22000,
                "etd": "1",
                "note": ""
              }
            ]
          }
        ]
      },
      {
        "code": "jne",
        "name": "Jalur Nugraha Ekakurir (JNE)",
        "costs": [
          {
            "service": "OKE",
            "description": "Ongkos Kirim Ekonomis",
            "cost": [
              {
                "value": 18000,
                "etd": "2-3",
                "note": ""
              }
            ]
          },
          {
            "service": "REG",
            "description": "Layanan Reguler",
            "cost": [
              {
                "value": 20000,
                "etd": "1-2",
                "note": ""
              }
            ]
          },
          {
            "service": "YES",
            "description": "Yakin Esok Sampai",
            "cost": [
              {
                "value": 30000,
                "etd": "1-1",
                "note": ""
              }
            ]
          }
        ]
      },
      {
        "code": "tiki",
        "name": "Citra Van Titipan Kilat (TIKI)",
        "costs": [
          {
            "service": "SDS",
            "description": "Same Day Service",
            "cost": [
              {
                "value": 135000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "HDS",
            "description": "Holiday Delivery Service",
            "cost": [
              {
                "value": 49000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "ONS",
            "description": "Over Night Service",
            "cost": [
              {
                "value": 26000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "REG",
            "description": "Regular Service",
            "cost": [
              {
                "value": 17000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "ECO",
            "description": "Economi Service",
            "cost": [
              {
                "value": 14000,
                "etd": "",
                "note": ""
              }
            ]
          }
        ]
      }
    ]
  }
}

現在,我想獲取結果數據->成本->服務,然后從結果中獲得成本的價值->成本->成本->價值並將其附加在組合框上。

$.each(jsonStr['rajaongkir']['results'], function(i,n){
    cou = '<option value="'+n['costs']['description']+'">'+n['costs']['description']+'</option>';
    cou = cou + '';
    $("#service").append(cou);
});

我試圖運行上方代碼並獲得undefined值。

有什么辦法得到它嗎?

這似乎是一個動態系統,在動態系統中,使用固定索引訪問它是一個非常糟糕的主意。 我可能會誤解了您的問題,但這是一個非常靈活的解決方案。 使用具有ES6語法的快速映射功能,我進入了該數據集:

[ 
 [ { service: 'Surat Kilat Khusus', cost: 16500 },
   { service: 'Express Next Day', cost: 22000 } 
 ],
 [ { service: 'OKE', cost: 18000 },
   { service: 'REG', cost: 20000 },
   { service: 'YES', cost: 30000 } 
 ],
 [ { service: 'SDS', cost: 135000 },
   { service: 'HDS', cost: 49000 },
   { service: 'ONS', cost: 26000 },
   { service: 'REG', cost: 17000 },
   { service: 'ECO', cost: 14000 } 
 ] 
]

它具有一個決定每個項目的二維數組。 它是一個組合的“盒子”。 長度是動態的並且不是固定的,這里還可以通過前面的名稱將數據集設置為對象:

[ { 'POS_Indonesia_(POS)': [ [Object], [Object] ] },
  { 'Jalur_Nugraha_Ekakurir_(JNE)': [ [Object], [Object], [Object] ] },
  { 'Citra_Van_Titipan_Kilat_(TIKI)': [ [Object], [Object], [Object], [Object], [Object] ] } ]

我還確保從鍵中刪除空格,並用下划線替換它們。 這樣一來,無需提供空間來再次在js中獲取鍵,而無需跳過整個“字符串鍵”。

我的代碼看起來像這樣,但是請記住,有更有效的方法可以做到這一點:

我首先從提供的目標中解構數組,然后必須使用babel或其他現代編譯器進行編譯才能完成此工作。 我尚未檢查性能,但是外觀非常干凈,可以高度配置使用。

let [...costs] = somedata.rajaongkir.results;

costs.map(obj => {
 let [...costs] = obj.costs;
 return {
   [obj.name.split(' ').join('_')]: costs.map(obj => ({service:obj.service, cost: obj.cost[0].value}))
  }
});

由於“ costs”是一個數組,因此您希望使用以下方法通過索引訪問它:

n['costs'][0]['description']

暫無
暫無

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

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