开发者

Populating an array in javascript, what is the difference between those two method?

here is the code of test 1

array_one = [];
array_one[1] = 'image1.jpg';
array_one[2] = 'image2.jpg';
array_one[3] = 'image3.jpg';

and the code of test 2

array_two = ['image1.jpg', 'images2.jpg', 'images3.jpg'];

why the first one work... and the other on is not... in fac开发者_运维知识库t i am looking for a solution to populate an array, without having to assign the index.

note : dont talk about the [0] index, i just dont use it...


The index is zero based.

array_two = ['image1.jpg', 'images2.jpg', 'images3.jpg'];
array_two[0]
=> "image1.jpg"

and you also have a syntax error, it should be

array_one[1] = 'image1.jpg';
=> "image1.jpg"


Try it like this:

var array_two = ['','image1.jpg', 'images2.jpg', 'images3.jpg'];
alert(array_two);

Unless you have the following set and didn't write it in

var array_two = new Array();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜