1

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 ]
 ...
0

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.