0

I am working on a client-server project. As I writing the script in HTML. I have a hard time push the data I have to array. Here is the source code snippets:

    var latitude = [];
    async function getapi(url){
        const response = await fetch(url);
        var data = await response.json();
        show(data);
        return data;
    }
    
    getapi(api_url);
    function show(data){
        for (r in data){
            latitude.push(data[r]['DepLat'];
            var dep_marker = new mapboxgl.Marker({color: 'red'})
            .setLngLat([data[r]['DepLng'],data[r]['DepLat']])
            .addTo(map);
            var arr_marker = new mapboxgl.Marker({color: 'cyan'})
            .setLngLat([data[r]['ArrLng'],data[r]['ArrLat']])
            .addTo(map);
        }
    }

The result I have in latitude after calling show(data) is still empty. Thank you.

3
  • 3
    Please, try to explain better your problem. Commented Apr 26, 2021 at 2:43
  • what does data look like? Commented Apr 26, 2021 at 2:44
  • Did you wait for the function to actually execute? Tip: await. Commented Apr 26, 2021 at 2:50

1 Answer 1

1

You have a typo. The line where you push the value is missing the closing parentheses.

function show(data){
    for (r in data){
    // your code says .push(
         latitude.push(data[r]['DepLat'];
      // rest of your code
 }

Do it this way

function show(data){
    for (r in data){
        latitude.push(data[r]['DepLat']);
       // rest of your code
}
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.