Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
90
rated 0 times [  91] [ 1]  / answers: 1 / hits: 25573  / 10 Years ago, wed, september 10, 2014, 12:00:00

Is there a better way to get the bio object into the pushed chart 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. 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.


More From » jquery

 Answers
90

If your current code is working, then you're quite right that $.map can do the same job:



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):



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


Updated Fiddle


[#69499] Monday, September 8, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aileenreynap

Total Points: 140
Total Questions: 106
Total Answers: 99

Location: Andorra
Member since Sun, Oct 18, 2020
4 Years ago
;