有没有更好的方法将bio
对象放入推送的图表values
?
// date and value keys.
var data = {"1":[{"date":"2014-03-10","value":14},{"date":"2014-03-17","value":15},{"date":"2014-03-19","value":13},{"date":"2014-04-11","value":11},{"date":"2014-04-13","value":13.7},{"date":"2014-04-14","value":14.6},{"date":"2014-04-15","value":17},{"date":"2014-04-17","value":9},{"date":"2014-04-20","value":10},{"date":"2014-04-21","value":17},{"date":"2014-04-24","value":15},{"date":"2014-05-02","value":10},{"date":"2014-05-03","value":95.3},{"date":"2014-05-09","value":92.1},{"date":"2014-05-12","value":3},{"date":"2014-05-14","value":88.9},{"date":"2014-05-15","value":95.3},{"date":"2014-05-23","value":82.6},{"date":"2014-05-24","value":95.3},{"date":"2014-05-30","value":99},{"date":"2014-05-31","value":88.9},{"date":"2014-06-01","value":80},{"date":"2014-06-17","value":82},{"date":"2014-07-08","value":95},{"date":"2014-07-30","value":127},{"date":"2014-08-02","value":90},{"date":"2014-08-03","value":80},{"date":"2014-08-09","value":82}],"2":[{"date":"2014-03-10","value":"1"},{"date":"2014-03-19","value":"23"},{"date":"2014-04-11","value":"14"},{"date":"2014-04-13","value":"14.4"},{"date":"2014-04-14","value":"14"},{"date":"2014-04-21","value":"11"},{"date":"2014-04-24","value":"13.5"},{"date":"2014-05-04","value":"4"},{"date":"2014-05-15","value":"15"},{"date":"2014-05-17","value":"16"},{"date":"2014-05-23","value":"9.3"},{"date":"2014-05-24","value":"11"},{"date":"2014-05-25","value":"14.9"},{"date":"2014-06-01","value":"14.1"}]};
var bio = [];
$.each(data['1'], function(date, value) {
bio.push({x: value['date'], y: Math.round(value['value'])});
});
chart = [];
chart.push({
area: true,
color: '#FFBA78',
values: bio,
});
基本上我正在做的是获取数据[' 1']数组,并且对于每个节点,我将密钥date
更改为x
}和value
进入y
。我想这可能是阵列图吗?类似的东西:
chart.push({
area: true,
color: '#FFBA78',
values: data['1'].map(function() { ... }),
});
答案 0 :(得分:1)
如果您当前的代码正常运行,那么$.map
可以完成相同的工作是完全正确的:
chart.push({
area: true,
color: '#FFBA78',
values: $.map(data[1], function(entry) {
return {x: entry.date, y: Math.round(entry.value)};
})
});
或者使用ES5中的Array#map
(可以在较旧的浏览器上进行填充):
chart.push({
area: true,
color: '#FFBA78',
values: data[1].map(function(entry) {
return {x: entry.date, y: Math.round(entry.value)};
})
});
答案 1 :(得分:1)
使用ES5 Array.prototype.map
:
data['1'].map(function(item){ return {x: item.date, y: Math.round(item.value)} } )
item
代表数组中的date
,value
对象。
重要提示:如果您需要在旧版本的IE中运行,请使用IE9 +,请按照填充说明here进行操作。
您还可以在这里查看2之间的一些性能工作台:
http://jsperf.com/jquery-map-vs-array-map
浏览器支持和性能可能是您要查看此选项的关键区域。