我有来自PHP的数据。
下面是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": ""
}
]
}
]
}
]
}
}现在,我希望获得有关结果的数据,->成本,->服务,然后从结果获取成本值,->成本,->成本->值,并将其附加到combobox中。
$.each(jsonStr['rajaongkir']['results'], function(i,n){
cou = '<option value="'+n['costs']['description']+'">'+n['costs']['description']+'</option>';
cou = cou + '';
$("#service").append(cou);
});我试图运行以上的代码并获得undefined值。
有什么办法能弄到吗?
发布于 2018-04-15 13:23:18
因为“成本”是一个数组,所以您希望使用以下索引访问它:
n['costs'][0]['description']发布于 2018-04-15 18:46:10
这似乎是一个动态系统,而在动态系统中,使用固定的索引访问它将是一个非常糟糕的主意。也许我已经回答了你的问题,但这里有一个相当灵活的解决方案。通过使用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 }
]
]这有一个二维数组,它对每个项目都有影响。它是一个组合的“盒子”。长度是动态的,不是固定的,这里还可以通过前面的名称将dataset设置为对象:
[ { '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}))
}
});https://stackoverflow.com/questions/49842161
复制相似问题