views:

61

answers:

2

By default index of every javascript array starts from 0. I want to create an array whose index starts from 1. I know, must be very trivial...thnx for ur help

+4  A: 

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).

cletus
Impossible...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.
detj
A: 
var foo = [];
foo[ 1 ] = 'something';

// foo = [undefined, "something"]

var s = "";
for (var i in foo) {
    s += i + ", "
}

// s = "1" (loop executes once)

var s = "";
for (var i = 0; i < foo.length; i++) {
    s += i + ", "
}

// s = "0, 1"  (loop executes twice without error)
Salman A