gpt4 book ai didi

javascript - JS : mapping an array inside a push

转载 作者:行者123 更新时间:2023-11-30 17:17:43 25 4
gpt4 key购买 nike

是否有更好的方法将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']数组,并且对于每个节点,我正在更改键:datex,并且value 转换为 y。我认为这可以通过数组映射实现?像这样的东西:

chart.push({
area: true,
color: '#FFBA78',
values: data['1'].map(function() { ... }),
});

Here's a JSFiddle.

最佳答案

如果您当前的代码可以正常工作,那么您认为 $.map 可以完成相同的工作是完全正确的:

chart.push({
area: true,
color: '#FFBA78',
values: $.map(data[1], function(entry) {
return {x: entry.date, y: Math.round(entry.value)};
})
});

Updated Fiddle

或者使用来自 ES5 的 Array#map(可以在旧浏览器上填充):

chart.push({
area: true,
color: '#FFBA78',
values: data[1].map(function(entry) {
return {x: entry.date, y: Math.round(entry.value)};
})
});

Updated Fiddle

关于javascript - JS : mapping an array inside a push,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25776222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com