1

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.

3
  • You could just toy around with different ways to do different things (how will you create the array, how will you create the elements that are going to go into the array, how will you actually insert those elements, etc.) and then put the various techniques on jsperf.com Commented Sep 3, 2011 at 7:19
  • micro optimizations are the root of all evil programmers.stackexchange.com/questions/80084/… Commented Sep 3, 2011 at 8:20
  • I managed to write and finished the code for a web tool to perform the tasks that I set out to do. But as I learned more, I want it to be better and faster for users, so now I am scrapping most of the code and rewrite to fit new algorithm. I don't think I will ever finish. Commented Sep 3, 2011 at 9:07

5 Answers 5

2

Array indexes in javascript are always in the interval [0, num_elements). If you do not want this, you need to use an object. Then you have arbitraty indexes but no fixed order.

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

1 Comment

thanks, I guess I was just asking if there is some way to declare something like arrayx {15,0000,20}, but I guess not.
1

Without loop you can only use instant initialization:

var arr = [null, null, null, ... 15 times ...., '000000', '000000', '000000', '000000', '000000'];

But probably it's better to use loop :)

4 Comments

Note that having a property with value null, or indeed undefined, is not quite the same as having no property under a given index.
yes, I know it. I've choose the null because it's shorter to write for example :)
Shorter would be: [,,,,,,,,,,,,,,,'000000', '000000',...]
thanks, I guess I was just asking if there is some way to declare something like arrayx {15,0000,20}, but I guess not.
0

It's better to use an object, if you want to use an offset:

var iOffset = 15;
var iCount = 20;
var oValues = {};
for (var i = iOffset ; i < iOffset + iCount; i++) {
    oValues[i] = 0;
}

1 Comment

thanks, this seem to be the alternative to array. I guess I was just asking if there is some way to declare something like arrayx {15,0000,20}, but I guess not.
0

For the specific case where each item in the array is a string, you can do it without a loop with the new Array(length) constructor and a sneaky join:

new Array(15).concat(('000000'+new Array(20).join(',000000')).split(','))

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.

1 Comment

thanks, this seem to be the alternative to array. I guess I was just asking if there is some way to declare something like arrayx {15,0000,20}, but I guess not.
0

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:

var a = [];
a[15] = '000';
alert(a.length); //=> 16

You could use a recursive function to create an array of 20 elements, something like:

function arr(len){
    return len ? arr(len-1).concat('0000') : [];
}
var myArr = arr(20);

Or create an array of small objects, containing your custom index:

function arr(len){
    return len ? arr(len-1).concat({index:len+14,value:'0000'}) : [];
}
var myArr = arr(20);
alert(myArr[0].index); //=> 15

Alternatively, using a loop, you can do something like:

var nwArr = function(len,start){
  var l = len+start, a = Array(l);
  while ((l-=1)>=start){ a[l] = '0000'; }
  return a;
}(20,15);
alert(nwArr[15]); //=> '0000'
alert(nwArr[0]);  //=> undefined

1 Comment

thanks, I will keep these answers in mind, I think one of these is handy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.