I have this loop, the api returns markers fine, i want to add the url and the image inside the array.
let coordsList = [];
for (let i in markers) {
let coords = markers[i].petLost.lostCoords;
let url = petUrl + markers[i]._id;
let image = petImage + markers[i].imageURL;
coordsList.push(coords);
}
The result with this loop is:
[ 24.728216, 36.2308272 ] , [ 27.728216, 33.2308272 ] , ...
How can i inject in there the url and the image for each corresponding item in the array?
Desired result:
[ 24.728216, 36.2308272, http://blabla.html, http://image.jpg ] ,
[ 27.728216, 33.2308272, http://blabla2.html, http://image2.jpg ]
...
You could just try this:
let coords = markers[i].petLost.lostCoords;
let url = petUrl + markers[i]._id;
coords.push(url)
let image = petImage + markers[i].imageURL;
coords.push(image);
coordsList.push(coords);
The above snippet can become more elegant as below:
let markers = markers[i];
let markerData = markers.petLost
.lostCoords
.push(petUrl + markers._id)
.push(petImage + markers.imageURL);
markersList.push(markerData);
Note I did a rename, in order to make it more meaningful and readable. If you follow this approach, you should replace the coordsList
, wherever it is used with the markersList
.
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.