Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to add data to array in javascript dynamically

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

PREVIOUS NEXT
Code Example
Javascript :: javascript unselect radio 
Javascript :: jquery filter data 
Javascript :: how to get day, month and year javascript 
Javascript :: how to find out most repeated string in an array js 
Javascript :: this.setstate is not a function 
Javascript :: how to print in a same line in javascript 
Javascript :: javascript resize window 
Javascript :: how to write a program that shows a random number between 1 and 100 in your browser 
Javascript :: how to add comma in react map 
Javascript :: hide the js code from source 
Javascript :: mobile number validation in javascript with country code 
Javascript :: window change detect 
Javascript :: javascritp canvas 
Javascript :: react using proptypes 
Javascript :: how to change the first 3 letters from a string toupper case 
Javascript :: react native password strength meter 
Javascript :: Is date greater than 18 years old javascript 
Javascript :: compare two dates and sort array of objects 
Javascript :: Example: Export a Variable in Node 
Javascript :: lodash reduce 
Javascript :: static variable in javascript 
Javascript :: define a class in javascript 
Javascript :: enable vue devtools 
Javascript :: jest debugger node 
Javascript :: promise.all 
Javascript :: config mode en webpack 
Javascript :: usecontext hook react 
Javascript :: jest wait for timeout 
Javascript :: angular how to check a radiobox 
Javascript :: c# razor for loop javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =