What's the most efficient way to create this simple array dynamically.
var arr = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
Let's say we can get the number 10 from a variable
var mynumber = 10;
Like all scripting languages, JavaScript has dynamic arrays: their size is not predetermined, nor the type of data.
There are two ways to dynamically add an element to the end of a JavaScript array. You can use the Array. prototype. push() method, or you can leverage the array's “length” property to dynamically get the index of what would be the new element's position.
var arr = []; for(var i=1; i<=mynumber; i++) { arr.push(i.toString()); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With