2

I want to design some arrays like [1..25] in JavaScript, but I don't want to do by hand ([1, 2, 3, 4, 5, 6, [...], 25]).

How would you do?

5
  • You'll need to iterate one way or another. Using a for loop should be simple enough. Commented Aug 2, 2014 at 20:00
  • 2
    I want to know about the -1: why? I don't know there is a range() function in another language to be equivalent in JS. If I know how to search for a simple thing like this, I probably didn't create a topic. And another thing: what's wrong with my question? The negatives are because it is "duplicate"? Commented Aug 2, 2014 at 20:06
  • Negatives are most likely because you've shown no effort in solving the problem. A simple Google search would lead you to answers. Commented Aug 2, 2014 at 20:10
  • @cookiemonster Thank you for clarification, but it's not truth about my effort. I didn't know how to search: what can I do? :< Commented Aug 2, 2014 at 20:12
  • For future reference, "JavaScript create array of numbers" (without the quotes) gives useful results in Google. Commented Aug 2, 2014 at 20:15

5 Answers 5

7

Well you could make a simple function...

function range(min, max) {
  var len = max - min + 1;
  var arr = new Array(len);
  for (var i=0; i<len; i++) {
    arr[i] = min + i;
  }
  return arr;
}

range(1,10);
// [1,2,3,4,5,6,7,8,9,10]

This answer is not the smallest amount of code, but it's very readable and tremendously faster than any other solution provided here.

Sign up to request clarification or add additional context in comments.

5 Comments

new Array(len) can be just [].
@Derek朕會功夫, it's way faster to set the size of the array if it's known. See some benchmarks.
Well if you are into performance than definitely new Array(len) is better.
Are you not into writing well-performing code? For what it's worth, I use [] when I'm working with an array of unknown size, but why would you give up tons of speed just to write [] instead of new Array(len)?
True, you are absolutely right.
1

If you are interested in shortness, create a small helper function

function range(begin, end){
   var arr = [];
   for(var i=begin; i<=end; i++){
       arr.push(i);
   }
   return arr;
}

var myArray = range(1, 25);

Comments

1

If the your elements are related (the incrementation is fixed fro instance), you can do this with a loop:

for(var i = 1; i<=25; i++) {myArray.push(i);}

3 Comments

that should be a for loop not a while.
while doesn't work that way.
I know, it was just a mistake by inadvertence
0

You could add the numbers in a loop eg:

var array = [];
for (var i=1; i<26; i++) {
    array[i-1] = i;
}

1 Comment

Just do var i=1; i<=25 imo
0
var min = 1, max = 25;
Array.apply(null, Array(max - min + 1)).map(function (i,j) {
    return j + min;
});

Look ma, no for or while! Set it into Array for convenience: (Using Array as namespace)

Array.range = function(min, max){
    return this.apply(null, this(max - min + 1)).map(function (i,j) {
        return j + min;
    });
};

Array.range(1, 25);    //[1..25]

2 Comments

A lot of magic at the cost of a lot of performance.
@maček - Just for fun. (Because I thought this question is going to be closed for duplicate)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.