0

I was wondering if it's possible to get a sequence of pictures into an array. I'd like to use plain JavaScript, because I have zero experience in PHP or any other language to achieve this.

So I created a map called "images", which contains 50 images. The first one is called: "1", the second one is called: "2" and so on. They all are the same type (.jpg).

I can do this manually like:

var pictures = new Array();
pictures[0] = "images/1.jpg";
pictures[1] = "images/2.jpg";
//and so on

But only a mad man would do this. also when I upload a new picture to the "images" folder, I have to manually add the new image to the array, so I was thinking about a while loop which checks if each image in the folder is stored into the array.

4

3 回答 3

0
var arr = [];
for (var i = 0, max = 50; i < max; i += 1) {
    arr[i] = "images/" + i + ".jpg";
}

如果您想要更改图像数量,请尝试以下操作:

function bar (numberOfImages) {
  var arr = [];
  for (var i = 0; i < numberOfImages; i += 1) {
    arr[i] = "images/" + i + ".jpg";
  }
于 2013-08-10T15:57:34.143 回答
0

你可以试试:

var pictures = new Array();
for(var x=1; x<51; x++ ) {
  pictures[x-1] = "images/"+x+".jpg";
}
于 2013-08-10T15:57:59.787 回答
0
var numberOfImages = 50; // or whatever
var im, pictures = new Array();
for (var i = 0; i < numberOfImages ; i++) {
    im = "images/" + i + ".jpg";
    pictures.push(im);
}
于 2013-08-10T15:58:14.400 回答