我的JSON返回此信息:
DESCRIPTION
我需要这个,在data.js到highcharts graphic。
[{"label": "Yes", "value": 26}, {"label": "No", "value": 21}]
列表内部列表。
我试试:
[["Yes", 26], ["No", 21]]
但不会返回此结果:
var paid_data = new Array();
var paid_data2 = new Array();
$.getJSON(url, function(res){
console.log(res);
// Transform dict to list
for (i = 0; i < 2; i++) {
paid_data.push(res[i]['label'],res[i]['value']);
};
// List inner list
for (i = 0; i < 4; i++) {
paid_data2.push(paid_data[i]);
paid_data2.push(paid_data[i+1]);
}
console.log(paid_data);
答案 0 :(得分:3)
使用ES6的Arrow Function
obj.map(el => [el.label, el.value]);
var obj = [{"label": "Yes", "value": 26}, {"label": "No", "value": 21}];
var newObj = obj.map(el => [el.label, el.value]);
document.body.innerHTML = '<pre>' + JSON.stringify(newObj, 0, 4) + '</pre>';
ES5中的相同代码
obj.map(function (el) {
return [el.label, el.value]
});
var obj = [{
"label": "Yes",
"value": 26
}, {
"label": "No",
"value": 21
}];
var newObj = obj.map(function (el) {
return [el.label, el.value]
});
console.log(newObj);
document.body.innerHTML = '<pre>' + JSON.stringify(newObj, 0, 4) + '</pre>';