简体   繁体   English

JS:在推送中映射数组

[英]JS: mapping an array inside a push

Is there a better way to get the bio object into the pushed chart values ? 是否有更好的方法将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,
});

Basically what I'm doing is taking the data['1'] array, and for each node I'm changing the keys: date into x , and value into y . 基本上,我正在做的是使用data ['1']数组,并且对于每个节点,我都在更改键: date更改为xvalue更改为y I think this may be possible with an array map? 我认为这可能与数组映射? Something like: 就像是:

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

Here's a JSFiddle. 这是一个JSFiddle。

If your current code is working, then you're quite right that $.map can do the same job: 如果您当前的代码正常工作,那么您可以肯定$.map可以完成相同的工作:

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

Updated Fiddle 更新小提琴

Or using Array#map from ES5 (which can be shimmed on older browsers): 或使用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 更新小提琴

Using ES5 Array.prototype.map : 使用ES5 Array.prototype.map

data['1'].map(function(item){ return  {x: item.date, y: Math.round(item.value)}  } )

Where item represent the date , value object in your array. 其中item表示数组中的datevalue对象。

Important: this will work on IE9+ if you need this to run in older version of IE please follow the polyfill instructions here . 重要提示:如果您需要在IE9 +上运行该版本,则该功能将在IE9 +上运行,请按照此处的polyfill说明进行操作。

You can also check some of the performance benchmakrs between the 2 here: 您还可以在此处检查2个之间的一些性能基准:

http://jsperf.com/jquery-map-vs-array-map http://jsperf.com/jquery-map-vs-array-map

The browser support and the performance are probably the key areas that you want to review in order to go for this option. 浏览器支持和性能可能是您想要查看以使用此选项的关键区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM