JSON array starting with bit 1
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?
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.
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.
精彩评论