The following string of json data is used to store the preloaded image path:
var imgData = [
{ name: "p1", src: "images/p1.jpg" },
{ name: "p2", src: "images/p2.jpg" },
{ name: "p3", src: "images/p3.jpg" },
{ name: "p4", src: "images/p4.jpg" },
{ name: "p5", src: "images/p5.jpg" }
]
The following function can get the path src of each line of json through the name of the line. Let us take a look at the code:
function getData(name) {
var picArr = imgData;
var picSrc;
for (var i = 0; i < picArr.length; i ) {
var cur_person = picArr[i];
if (cur_person.name == name) {
picSrc = cur_person.src;
}
}
return picSrc;
}
After the function is executed, the src of the line will be returned.
var g = getData("p1");
console.log(g);
After output, you can see the result: images/p1.jpg