How to create an array in JavaScript whose indexing starts at 1?
Solution 1
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).
Solution 2
A simple solution is to fill the zeroth item:
var map = [null, 'January', 'February', 'March'];
'First month : ' + map[1];
Semantically it would be better to use an object:
var map = {1:'January', 2:'February', 3:'March'};
'First month : ' + map[1];
Note these keys are not ints actually, object keys are always strings.
Also, we can't use dot notation for accessing. (MDN - Property Accessors)
I'd choose the first solution, which I think is less confusing.
Solution 3
Since this question also pops up for a Google search like "javascript start array at 1" I will give a different answer:
Arrays can be sliced. So you can get a sliced version of the Array like this:
var someArray = [0, 1, 2, 3];
someArray.slice(1);
[1, 2, 3]
someArray.slice(2, 4);
[2, 3]
Source: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
Solution 4
You could use delete to remove the first element like so:
let arr = ['a','b','c'];
delete arr[0];
console.log(arr[0]);
console.log(arr[1]);
Or just not define it at all:
let arr = [,'b','c'];
console.log(arr[0]);
console.log(arr[1]);
If you want to make sure that you always get the first truthy element regardless of the index and have access to ES6 you can use:
arr.find(Boolean)
detj
!''# ^"`$$- !=@$_ %*~4 &[]../ |{,,SYSTEM HALTED How to read Waka waka bang splat tick tick hash, Caret quote back-tick dollar dollar dash, Bang splat equal at dollar under-score, Percent splat waka waka tilde number four, Ampersand bracket bracket dot dot slash, Vertical-bar curly-bracket comma comma CRASH
Updated on February 04, 2022Comments
-
detj about 2 years
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.
-
detj almost 14 yearsImpossible...dats surprising! Well, there's no useful reason for me to say...why I want to start at 1...just an idea. So, yeah..i guess, I have to adjust my indices accordingly, which is not a problem. Wanted to start at 1, just for the sake of it.
-
Michał Perłakowski over 7 yearsThis doesn't work with many array methods. For example,
Array.from(myArray.keys())
returns[0, 1, 2, 3, 4]
instead of[1, 2, 3, 4, 5]
. -
Ramin Bateni over 7 years@Gothdo, i think you're wrong! Because this result >>
[0, 1, 2, 3, 4]
has not any unexpected value for @detj (questioner). He just need([0, 1, 2, 3, 4])[n]
whenn>=1
.So this array always has correct result for him. -
xpuu about 6 yearsNow, 8 years later, you can use Map
-
josefdev almost 6 yearsHow do I ignore index 0 in Javascript?
-
Matt Korostoff over 4 yearsThis just creates an array whose first value is 1. This question was about how to create an array where the first index is 1, which is a very different thing.
-
ToolmakerSteve over 4 years@josefdev - put a value there, and then never refer to it. I.E.
a = ["ignore me", "this has index 1", "another string, at index 2"]; firstString = a[1];
However, this advice isn't very useful - built in functions such asfor (s of a)
won't 'know' that you want to ignorea[0]
- the burden is entirely on you to find ways to code that don't use a[0]; it is more practical to get used to arrays starting at 0. -
General Grievance about 2 yearsSorry, but I'm not entirely sure what this is suggesting. It looks like you already have the 1-indexed array before you use
map
.