"data": [{
"Nic": {
"2017-09":6473,
"2017-10":66,
"2017-11-01":69571
},
"Nancy": {
"2017-09":185,
"2017-10":194,
"2017-11":2031
}
}]
我想要
series: [{
dateValue: '2017-09',
data: [6473, 185]
}, {
dateValue: '2017-10',
data: [66, 194]
}, {
dateValue: '2017-11',
data: [69571, 2031]
}]
答案 0 :(得分:0)
您可以使用解决方案
var data = [{
"Nic": {
"2017-09":6473,
"2017-10":66,
"2017-11":69571
},
"Nancy": {
"2017-09":185,
"2017-10":194,
"2017-11":2031
}
}];
var series = [];
var keys = Object.keys(data[0]);
var dates = Object.keys(data[0][keys[0]]);
$.each(dates, function(i){
var temp = {};
temp["dateValue"] = dates[i];
temp["data"] = [];
$.each(keys, function(j){
temp["data"].push(data[0][keys[j]][dates[i]]);
});
series.push(temp);
});
console.log(series);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
希望这会对你有所帮助。
答案 1 :(得分:0)
这是解决方案。
我使用underscorejs,我建议您使用它。但是,您可以轻松地将其转换为jQuery。
var data = [{
"Nic": {
"2017-09":6473,
"2017-10":66,
"2017-11":69571
},
"Nancy": {
"2017-09":185,
"2017-10":194,
"2017-11":2031
}
}];
var d = data[0];
var k = Object.keys(d);
var v = [];
_.forEach(k, function(ky){v.push(d[ky])});
var f = [];
_.forEach(v, function(vl){
var kys = Object.keys(vl);
_.forEach(kys, function(ky){
var val =_.findWhere(f, {dateValue: ky});
if(!val){
val = {dateValue: ky, data: []};
f.push(val);
}
val.data.push(vl[ky]);
});
});
document.getElementById("val").innerHTML = JSON.stringify(f);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<div id="val">
</div>
&#13;