简体   繁体   English

jQuery jSon获取具有多个值的子数组

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

I have data getting from PHP Curl jSon. 我有从PHP Curl jSon获取的数据。

Below is the sample of jSon data. 以下是jSon数据的示例。

Here is the data: 数据如下:

{
"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": ""
              }
            ]
          }
        ]
      }
    ]
  }
}

Now I want to get data on results -> costs -> service then get value of cost from results -> costs -> cost -> value and append it on combobox. 现在,我想获取结果数据->成本->服务,然后从结果中获得成本的价值->成本->成本->价值并将其附加在组合框上。

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

I was trying to run aboveo code and got undefined value. 我试图运行上方代码并获得undefined值。

Is there any way how to get it? 有什么办法得到它吗?

This seems like a dynamic system and in a dynamic system it would be quite a bad idea to access it with a fixed index. 这似乎是一个动态系统,在动态系统中,使用固定索引访问它是一个非常糟糕的主意。 There might be I missunderstood your question, but here is a solution that is quite flexible. 我可能会误解了您的问题,但这是一个非常灵活的解决方案。 With a quick mapping function with ES6 syntax i got to this dataset out: 使用具有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 } 
 ] 
]

This has a two dimensional array indecating each item. 它具有一个决定每个项目的二维数组。 It is a combined "box". 它是一个组合的“盒子”。 The length is dynamic and not fixed and here one could also get the dataset set to an object by the preceeding name: 长度是动态的并且不是固定的,这里还可以通过前面的名称将数据集设置为对象:

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

I also made sure to remove whitespaces from the keys and replaced them with an underscore. 我还确保从键中删除空格,并用下划线替换它们。 This way one wouldnt need to provide spaces for getting the keys again in js, skipping the whole 'string key'. 这样一来,无需提供空间来再次在js中获取键,而无需跳过整个“字符串键”。

My code looks like this, but keep in mind there are more efficient ways of doing this: 我的代码看起来像这样,但是请记住,有更有效的方法可以做到这一点:

I start with deconstructing the array from provided target, you must transpile with babel or some other modern transpiler to make this work. 我首先从提供的目标中解构数组,然后必须使用babel或其他现代编译器进行编译才能完成此工作。 I have not checked performance, but it is very clean to look at and highly configurable to work with. 我尚未检查性能,但是外观非常干净,可以高度配置使用。

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