I have 2 arrays
var labels = ["DESKTOP","MOBILE","TABLET"]
var chartData = ["100","10","15"]
And I need to bine these into one array with objects
var myData = [{
label: DESKTOP,
value: 100},
{
label: MOBILE,
value: 10},
{
label: TABLET,
value: 15},
];
So far I've pushed labels into an array with new object
$.each(labels, function (index, item) {
myData.push({
label: item,
value: ''
});
});
I've done empty value, and now can't push value to an object in array. Just can't figure out how to push each value to new object in array. Help is much appreciated.
Thanks.
Data is sample only.
I have 2 arrays
var labels = ["DESKTOP","MOBILE","TABLET"]
var chartData = ["100","10","15"]
And I need to bine these into one array with objects
var myData = [{
label: DESKTOP,
value: 100},
{
label: MOBILE,
value: 10},
{
label: TABLET,
value: 15},
];
So far I've pushed labels into an array with new object
$.each(labels, function (index, item) {
myData.push({
label: item,
value: ''
});
});
I've done empty value, and now can't push value to an object in array. Just can't figure out how to push each value to new object in array. Help is much appreciated.
Thanks.
Data is sample only.
Share Improve this question asked Apr 5, 2016 at 2:37 EvgenyKEvgenyK 651 silver badge8 bronze badges2 Answers
Reset to default 3
var labels = ["DESKTOP", "MOBILE", "TABLET"];
var chartData = ["100", "10", "15"];
var myData = [];
labels.forEach(function(e, i) {
myData.push({
label: e,
data: chartData[i]
})
})
document.write(JSON.stringify(myData));
How about:
$.each(labels, function (index, item) {
myData.push({
label: item,
value: chartData[index]
});
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745575810a4633956.html
评论列表(0条)