简体   繁体   中英

How do I put arrays which consist of variables that are attached to URL's into image placeholders? JavaScript

var imagesID = ["Image1", "Image2", "Image3", "Image4", "Image5", "Image6"];

function setImage() {
  for (var i = 0; i < gardenList.length; i++) {
    image(imagesID[i]++, gardenList[i]++);
  }
} 

gardenList consists of a list of variables that are chosen by the user and are added to the end of an array as the user checks boxes. What I'm having trouble with is that when the function loops for as long as the length of gardenList.Length, the program says that image() id parameter value (NaN) is not a string. "Image1" and so on are the IDs for image placeholders. The variables in gardenList consist of URLs and would look something like this:

var lettuce = " https://bhvifbvijcncjowcw " gardenList = [lettuce, ...] Does anyone know how to resolve this issue?

You are adding unnecessary ++ in the loop. This should work

 var imagesID = ["Image1", "Image2", "Image3", "Image4", "Image5", "Image6"]; function setImage() { for (var i = 0; i < gardenList.length; i++) { image(imagesID[i], gardenList[i]); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM