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? iItried 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];
}
You have to instantiate the object first. The simplest way is:
Or an other, less concise way, but closer to your original code:
array()will not create a new array (unless you defined that function). EitherArray()ornew Array()or just[].I recommend to read the MDN JavaScript Guide.