Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - Create Simple Dynamic Array

Tags:

javascript

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 image 428
Sam Avatar asked May 04 '12 15:05

Sam


People also ask

Does JavaScript have dynamic arrays?

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

How do you dynamically add an element to an array in typescript?

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.


1 Answers

var arr = []; for(var i=1; i<=mynumber; i++) {    arr.push(i.toString()); } 
like image 145
Nadh Avatar answered Oct 19 '22 10:10

Nadh