I want to create an array in javascript to have 20 elements with a starting index of 15, assign every element with a value of 00000. What is the most efficient way, and can it be done without looping through the index?
TIA.
I want to create an array in javascript to have 20 elements with a starting index of 15, assign every element with a value of 00000. What is the most efficient way, and can it be done without looping through the index? TIA.
| |||||||
feedback
|
It's better to use an
| |||
feedback
|
Array indexes in javascript are always in the interval | |||
feedback
|
Without loop you can only use instant initialization:
But probably it's better to use loop :) | |||||||||
feedback
|
For the specific case where each item in the array is a string, you can do it without a loop with the
Unlikely to be faster than the loop though... certainly less clear. I'd also question whether an Array with missing properties 0–14 is something it's generally sensible to have. | ||||
feedback
|
You can't have an array starting with an index of 15. The index of an array always start with index 0, as is demonstrated with:
You could use a recursive function to create an array of 20 elements, something like:
Or create an array of small objects, containing your custom index:
Alternatively, using a loop, you can do something like:
| ||||
feedback
|