あなたの例では、各画像パス/ファイル名が何であるかを知る何らかの方法が必要です (それらは IMG_001.jpg、002.jpg などではないため)。簡単ですがローテクな方法は、すべてのファイル名を配列にパックして、ソース情報として機能させることです。
//Pack the image filenames into an array using Array shorthand
var imageFiles = ['IMG_0019.jpg', 'IMG_0020.jpg', 'IMG_0021.jpg', 'IMG_0022.jpg'];
次に、その配列内の各要素をループして、それぞれの画像要素を作成します。image 要素を作成し、1 つのステップで最終的な配列にパックします。
//Loop over an array of filenames, and create an image for them, packing into an array:
var pictures = []; //Initialise an empty array
for (var i = 0, j = imageFiles.length; i < j; i++) {
var image = new Image; //This is a placeholder
image.src = 'images/' + imageFiles[i]; //Set the src attribute (imageFiles[i] is the current filename in the loop)
pictures.push(image); //Append the new image into the pictures array
}
//Show the result:
console.log(pictures);
これは効率的ではなく、理解しやすいように書かれたコードです。特に、for (imageFiles の i) はより効率的に実行できますが、このタイプのループの利点は、あらゆるもの (オブジェクト、配列、文字列) で使用できることです。これは、学習中の優れた汎用ツールです。for x in yループが問題を引き起こす可能性がある理由については、@Web_designer のリンクされた質問を参照してください。ここでの for ループの構文は、JS の配列ループの「古典的なバニラ」とほぼ同じです。
また、画像ファイル名が常に数値で連続している場合は、それを利用できますが、事前に保存するのではなく、「計算」します。
詳細が必要な場合はお知らせください。