Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add values to an array of objects dynamically in javascript?

this is an array of objects,

var data = [
      {"label" : "1", "value" : 12},
      {"label" : "1", "value" : 12},
      {"label" : "1", "value" : 12},
      {"label" : "1", "value" : 12}
    ];

how can i add values to these dynamically? i tried the below code but no success

var lab =["1","2","3", "4"];
var val = [42,55,51,22];
var data = new Array();
for(var i=0; i<4; i++){
   data[i].label = lab[i];
   data[i].value = val[i];    
}

anyone please.. thanks in advance

like image 208
Mujtaba Haider Avatar asked Oct 22 '11 08:10

Mujtaba Haider


People also ask

How do you add a value to an array dynamically?

Since the size of an array is fixed you cannot add elements to it dynamically. But, if you still want to do it then, Convert the array to ArrayList object. Add the required element to the array list.

How do you add a value to an array of objects?

Method 1: push() The push() function is a built-in array method of JavaScript. It is used to add the objects or elements in the array. This method adds the elements at the end of the array.

How do you add an element to an existing array?

By using ArrayList as intermediate storage:Create an ArrayList with the original array, using asList() method. Simply add the required element in the list using add() method. Convert the list to an array using toArray() method.

Are JavaScript arrays dynamic?

Like all scripting languages​​, JavaScript has dynamic arrays: their size is not predetermined, nor the type of data.


2 Answers

You have to instantiate the object first. The simplest way is:

var lab =["1","2","3"]; var val = [42,55,51,22]; var data = []; for(var i=0; i<4; i++)  {     data.push({label: lab[i], value: val[i]}); } 

Or an other, less concise way, but closer to your original code:

for(var i=0; i<4; i++)  {    data[i] = {};              // creates a new object    data[i].label = lab[i];    data[i].value = val[i];     } 

array() will not create a new array (unless you defined that function). Either Array() or new Array() or just [].

I recommend to read the MDN JavaScript Guide.

like image 168
Felix Kling Avatar answered Sep 24 '22 17:09

Felix Kling


In Year 2019, we can use Javascript's ES6 Spread syntax to do it concisely and efficiently

data = [...data, {"label": 2, "value": 13}] 

Examples

var data = [        {"label" : "1", "value" : 12},        {"label" : "1", "value" : 12},        {"label" : "1", "value" : 12},      ];        data = [...data, {"label" : "2", "value" : 14}]   console.log(data)

For your case (i know it was in 2011), we can do it with map() & forEach() like below

var lab = ["1","2","3","4"];  var val = [42,55,51,22];    //Using forEach()  var data = [];  val.forEach((v,i) =>      data= [...data, {"label": lab[i], "value":v}]  )    //Using map()  var dataMap = val.map((v,i) =>    ({"label": lab[i], "value":v})  )    console.log('data: ', data);  console.log('dataMap : ', dataMap);
like image 39
its4zahoor Avatar answered Sep 25 '22 17:09

its4zahoor