Reputation: 25338
I have the following arrays:
dates = ['2013-06-01', '2013-07-01', '2013-08-01', '2013-09-01', '2013-10-01', '2013-11-01']
values = [20, 10, 5, 5, 20, 25]
What I need to do is merge them into this format:
data: [
{ date: '2013-06-01', value: 20 },
{ date: '2013-07-01', value: 10 },
{ date: '2013-08-01', value: 5 },
{ date: '2013-09-01', value: 5 },
{ date: '2013-10-01', value: 20 },
{ date: '2013-11-01', value: 25 }
]
Upvotes: 1
Views: 75
Reputation: 11807
hmm. I'm sure you already thought of this. using jquery, apologize if you don't use this.
data = jQuery.map(dates,function(v,i){
return {'date': i, 'value': values[i]}
}
Upvotes: 0
Reputation: 14453
var data = {
data: dates.map(function(x, i) {
return { date: x, value: values[i] };
})
};
Array.prototype.map is ECMAScript 5th Edition and doesn't work in < IE9. For older browsers us this polyfill
Upvotes: 0
Reputation: 3111
If jQuery is an option, you can try this:
http://api.jquery.com/serializeArray/
Upvotes: -1
Reputation: 104775
Assuming they're always the same length:
var data = [];
for (var i = 0; i < dates.length; i++) {
var obj = {}
obj.date = dates[i];
obj.value = values[i];
data.push(obj);
}
Upvotes: 3