add value to object in array javascript - Stack Overflow

I have 2 arraysvar labels = ["DESKTOP","MOBILE","TABLET"]var chartData =

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745575810a4633956.html

相关推荐

  • add value to object in array javascript - Stack Overflow

    I have 2 arraysvar labels = ["DESKTOP","MOBILE","TABLET"]var chartData =

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信