Is there a function in JavaScript similar to Python\'s range()?
I think there should be a better way than to write the following lines every time:
Here you go.
This will write (or overwrite) the value of each index with the index number.
Array.prototype.writeIndices = function( n ) {
for( var i = 0; i < (n || this.length); ++i ) this[i] = i;
return this;
};
If you don't provide a number, it will use the current length of the Array.
Use it like this:
var array = [].writeIndices(10); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]