views:

21

answers:

2

my JSON array is like this;

var foo = [{"aid":"1","atitle":"Ameya R. Kadam"},{"aid":"2","atitle":"Amritpal Singh"},{"aid":"3","atitle":"Anwar Syed"},{"aid":"4","atitle":"Aratrika"},{"aid":"5","atitle":"Bharti Nagpal"}]

if i select any element, for example,

alert(foo[0].atitle);

output: Ameya R. Kadam

the first element's atitle value shows up. i want to modify it so that the array starts from 1 instead of 0. like,

alert(foo[1].atitle);

output: Ameya R. Kadam

can it be done?

A: 

No. Javascript arrays starting at 0 is a design parameter of the Javascript language itself.

Instead, you could use foo[bar-1] if your bar variable holds an index numbered from 1, or you could have an empty/placeholder element at the beginning of the array.

But really, you should just get used to 0-indexed arrays if working with Javascript.

Amber
i am used to 0-indexed arrays. its just the design parameters of the current project need this particular array to be 1-index.
amit
A: 

First, this is almost certainly a mistake. If your programming language uses 0-indexed arrays, you should just accept that and move on.

Second, you could achieve a similar effect by pushing an empty element onto the front of your array.

Hank Gay
how do i push an empty element into the beginning of a JSON object?
amit
`var foo = [{},{"aid":"1","atitle":"Ameya R. Kadam"},...]`
Amber