Set a range of numbers as a variable (Javascript)
Solution 1
In addition to this answer, here are some ways to do it:
for
loop:
var numbers = []
for (var i = 1; i <= 100; i++) {
numbers.push(i)
}
Array.prototype.fill
+ Array.prototype.map
var numbers = Array(100).fill().map(function(v, i) { return i + 1; })
Or, if you are allowed to use arrow functions:
var numbers = Array(100).fill().map((v, i) => i + 1)
Or, if you are allowed to use the spread operator:
var numbers = [...Array(100)].map((v, i) => i + 1)
However, note that using the for
loop is the fastest.
Solution 2
Store the minimum and maximum range in an object:
var a = {
from: 0,
to: 100
};
Solution 3
Python
# There can be two ways to generate a range in python
# 1)
a = [*range(5)]
print(a)
#[0, 1, 2, 3, 4]
# 2)
a = [*range(5,10)]
print(a)
#[5, 6, 7, 8, 9]
Javascript
// Similar pattern in Javascript can be achieved by
let a;
// 1)
a = [...Array(5).keys()]
console.log(a) //[0, 1, 2, 3, 4]
// 2)
a = [...Array(5).keys()].map(value => value + 5);
console.log(a) //[5,6,7,8,9]
Solution 4
For what it's worth, @MaxZoom had answer that worked for my situation. However, I did need to modify the return statement to evaluate with && comparison. Otherwise appeared to return true for any number.
function Range(begin, end) {
this.low = begin;
this.hi = end;
this.has = function(n) {
//return this.low <= n <= this.hi;
return ( n >= this.low && n <= this.hi );
};
}
// code example
var alertRange = new Range(0,100);
var warnRange = new Range(101, 200);
var num = 1000;
if (alertRange.has(num)) {
alert("Number in ALERT range");
//Add Alert Class
}
else if (warnRange.has(num)) {
alert("Number in WARN range");
//Add Warn Class
}
else{
alert("Number in GOOD range")
//Default Class
}
Solution 5
Supported in all modern browsers including IE9+.
var numbers = Array.apply(null,Array(100)).map(function(e,i){return i+1;});
Comments
-
Johnny about 3 years
Let's say I want a variable to contain numbers from 1 to 100. I could do it like this:
var numbers = [1,2,3,4,...,98,99,100]
But it would take a bunch of time to write all those numbers down. Is there any way to set a range to that variable? Something like:
var numbers = [from 1, to 100]
This might sound like a really nooby question but haven't been able to figure it out myself. Thanks in advance.
-
Filipe over 8 yearsNot possible in Javascript.
-
rgajrawala over 8 years@Filipe Really? I can think of many different ways to do it.
-
Filipe over 8 years
-
zero298 over 8 yearsDoes it have to be an array? The only thing that I can think of similar to this would be an object
{from: 0, to: 100}
. That fits in a variable, but it is a constructed object that you would have to use by convention. JavaScript primitives can't really do this. -
MaxZoom over 8 yearsCheck this implementation
-
-
dewd over 8 yearsshame IE doesn't support fill. enjoying getting used to dropping all the IE8 restrictions to do cool things like this.
-
Admin over 8 yearsYou're trying to map using the index, but index is second param of map. Add v as value placeholder as first arg - current implementation with map returns an array of 100 NaNs
-
Selenir over 8 yearsI would recommend using
for
loop in this case, it's much faster: jsperf.com/range5254535 -
rgajrawala over 8 years@dewd You can use the polyfill.