-3

I need to get values of Z variable outside a for but when I print it in the console from inside the loop it gives correct values, while I printing it from outside the loop, it gives one value of value that supposed to be returned

fetch('http://open.mapquestapi.com/elevation/v1/profile?key=tHXSNAGXRx6LAoBNdgjjLycOhGqJalg7&shapeFormat=raw&latLngCollection='+profile)
          .then(r => r.json()) 
          .then(data => {
            var Z;
            for(var i=0;i<data.elevationProfile.length;i++){
                //console.log(data.elevationProfile[i].height);
                Z = (data.elevationProfile[i].height);
                //console.log(Z);
                }
                console.log(Z);
INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63
  • What did you expect the value to be then? – Teemu Mar 04 '19 at 09:16
  • i am calling values from this api via 5 clicks "for example". console.log from the loop gives all the 5 value. but console.log from outside the loop gives only one value. – Eslam Farag Mar 04 '19 at 09:18
  • 1
    You should avoid posting the `key` related to your mapquest account. It could very well be taken by someone here. I suggest editing this question and refreshing your key. – paddycorr Mar 04 '19 at 09:23

2 Answers2

0

The reason you see only one value outside of the loop is because everytime you loop you assign a new var to Z with =

try setting the Z outside the loop as an array and inside the loop push variables to the array

later on you would be able to console your array with all the values

somthing like this:

fetch('http://open.mapquestapi.com/elevation/v1/profile?key=*CENCOREDKEY*&shapeFormat=raw&latLngCollection='+profile)
      .then(r => r.json()) 
      .then(data => {
        var Z=[];
        for(var i=0;i<data.elevationProfile.length;i++){
            //console.log(data.elevationProfile[i].height);
            Z.push(data.elevationProfile[i].height);
            //console.log(Z);
            }
            console.log(Z);
fedesc
  • 2,554
  • 2
  • 23
  • 39
  • I have tried this before, but getting values in array won't be useful so i will bind these values into other array, so is there is any way to explode this array to be separated values? – Eslam Farag Mar 04 '19 at 09:29
  • there are many many manipulations you can make on arrays. it's not very clear what you are trying to do but datatypes in javascript are pretty easy to play with. you can turn your array to string, split it, slice it chew it anything you'd like. – fedesc Mar 04 '19 at 09:31
  • i need to add values that i have got in the end of this array this array: var latlngs = lats.map(function(e, i, o) {return [e, lngs[i], V];}); in order to creating GeoJSON file carries [lat, long, Z] – Eslam Farag Mar 04 '19 at 09:34
0

correct, when u console.log inside the for loop, it will show all the items of for the array. In your case, your overwriting Z variable every time the for loop executed, and at last, Z will have an item of last item being executed by for loop. That's why u get Z has only one value.

Let me know what u wanted to do !!!!,

If you want to save all the values of a height of value, then you must use array.

fetch('http://open.mapquestapi.com/elevation/v1/profile?key=*CENCOREDKEY*&shapeFormat=raw&latLngCollection='+profile)
  .then(r => r.json()) 
  .then(data => {
    var Z=[];
    for(var i=0;i<data.elevationProfile.length;i++){
        //console.log(data.elevationProfile[i].height);
        Z.push(data.elevationProfile[i].height);
        //console.log(Z);
        }
        console.log(Z);

where Z is array object and contains all Z height property in data.elevationProfile array.

Anand S
  • 790
  • 1
  • 7
  • 22
  • I have tried this before, but getting values in array won't be useful so i will bind these values into other array, so is there is any way to explode this array to be separated values? – Eslam Farag Mar 04 '19 at 09:31
  • there are many ways, just tell me what u wanted to do!!, so I can fix the issue for u – Anand S Mar 04 '19 at 09:49
  • i need to add values that i have got in the end of this array this array: var latlngs = lats.map(function(e, i, o) {return [e, lngs[i], V];}); in order to creating GeoJSON file carries [lat, long, Z] – Eslam Farag Mar 04 '19 at 11:04