Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an array in JavaScript whose indexing starts at 1?

By default the indexing of every JavaScript array starts from 0. I want to create an array whose indexing starts from 1 instead.

I know, must be very trivial... Thanks for your help.

like image 579
detj Avatar asked May 13 '10 12:05

detj


People also ask

How do I start an array index from 1?

Base Index of Java arrays is always 0. It cannot be changed to 1. Show activity on this post. You can use pointers, to jump to a certain point of the array and start the array from there.

Does array [- 1 work in JavaScript?

As others said, In Javascript array[-1] is just a reference to a property of array (like length ) that is usually undefined (because it's not evaluated to any value).

What is index 1 in an array?

Zero-based array indexing is a way of numbering the items in an array such that the first item of it has an index of 0, whereas a one-based array indexed array has its first item indexed as 1. Zero-based indexing is a very common way to number items in a sequence in today's modern mathematical notation.

What is the starting index of an array JavaScript?

JavaScript arrays are zero-indexed: the first element of an array is at index 0 , the second is at index 1 , and so on — and the last element is at the value of the array's length property minus 1 .


1 Answers

It isn't trivial. It's impossible. The best you could do is create an object using numeric properties starting at 1 but that's not the same thing.

Why exactly do you want it to start at 1? Either:

  • Start at 0 and adjust your indices as necessary; or

  • Start at 0 and just ignore index 0 (ie only use indices 1 and up).

like image 162
cletus Avatar answered Sep 21 '22 15:09

cletus