admin管理员组

文章数量:1022853

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 badges
Add a ment  | 

2 Answers 2

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]
  });     
 });

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 badges
Add a ment  | 

2 Answers 2

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]
  });     
 });

本文标签: add value to object in array javascriptStack Overflow