0

I have js code

var model = {
    start: newstart,
    end: newstop,
    imei: imei
};
$.ajax({
    url: dburl,
    dataType: 'json',
    type: 'GET',
    data: model,
    success: function (data) {
         speeddata = data;

         if (speeddata.length !== 0) {
             for (var i = 0; i < speeddata.length; i++) {
                 path = "path=" + speeddata[i].Latitude2 + ',' + speeddata[i].Longitude2;
                 var googleurl = "https://roads.googleapis.com/v1/speedLimits?"
                     + path + "&key=" + roadsapikey;
                 + path + "&key=" + roadsapikey;
                 $.ajax({
                     url: googleurl,
                     dataType: 'json',
                     type: 'GET',
                     success: function (data) {

                         for (var i = 0; i < data.speedLimits.length; i++) {
                             speedobject.push({
                                 speedlimits: data.speedLimits[i].speedLimit
                             });
                         }
                         console.log(speedobject);
                     }

                 });
             }

         }
    },
    error: function () {
        alert("Error");
    }
});

In this code I get data from db and write it to speeddata array, I have 6 elements in array.

   var speeddata = [{
Imei: 35745407257535, Latitude2: 50.9364, Longitude2: 3.12147, Speed: 8
},{
Imei: 35745407257535, Latitude2: 50.93918, Longitude2: 3.12485, Speed: 37
},{
Imei: 35745407257535, Latitude2: 50.93997, Longitude2: 3.12837, Speed: 7
},{
Imei: 35745407257535, Latitude2: 50.93834, Longitude2: 3.12893, Speed: 54
},{
Imei: 35745407257535, Latitude2: 50.9281, Longitude2: 3.13903, Speed: 56
},{
Imei: 35745407257535, Latitude2: 50.9219, Longitude2: 3.15888, Speed: 9
}];

After this I need to get speedlimit for every element in speeddata and write it to speedobject.

I try to it like here

 if (speeddata.length !== 0) {
             for (var i = 0; i < speeddata.length; i++) {
                 path = "path=" + speeddata[i].Latitude2 + ',' + speeddata[i].Longitude2;
                 var googleurl = "https://roads.googleapis.com/v1/speedLimits?"
                     + path + "&key=" + roadsapikey;
                 + path + "&key=" + roadsapikey;
                 $.ajax({
                     url: googleurl,
                     dataType: 'json',
                     type: 'GET',
                     success: function (data) {

                         for (var i = 0; i < data.speedLimits.length; i++) {
                             speedobject.push({
                                 speedlimits: data.speedLimits[i].speedLimit
                             });
                         }
                         console.log(speedobject);
                     }

                 });
             }

         }

But I have 6 repeats when console.log speedobject.

Here is array

Screen of google api array

So if I want to display it in table I will have 36 entries instead of 6

Where is my trouble?

Thank's for help.

12
  • Can you give a fiddle link and data object w/o the AJAX call? Commented Jan 22, 2018 at 14:32
  • One second, I will post it@Pavan Commented Jan 22, 2018 at 14:33
  • I cannot create fiddle, because it will have google roads api key. But I included speeddata data@Pavan Commented Jan 22, 2018 at 14:43
  • And what do you want? Commented Jan 22, 2018 at 14:47
  • After this I need to get speedlimit for every element in speeddata and write it to speedobject. I writed in post all info about me trouble @Pavan Commented Jan 22, 2018 at 14:48

2 Answers 2

1

As I understood it, you want to have 6 entries each with an array of speedlimits, like this:

success: function (data) {
    var speedLimits = []
    for (var i = 0; i< data.speedLimits.length;i++) {
        speedLimits.push(data.speedLimits[i].speedLimit);
    }
    speedobject.push({speedLimits: speedLimits})
    console.log(speedobject);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nope. I need need to get speedLimit property for every element in speeddata. I updated post, check it.
0

Update your code like

 if (speeddata.length !== 0) {
             for (var i = 0; i < speeddata.length; i++) {
                 path = "path=" + speeddata[i].Latitude2 + ',' + speeddata[i].Longitude2;
                 var googleurl = "https://roads.googleapis.com/v1/speedLimits?"
                     + path + "&key=" + roadsapikey;
                 + path + "&key=" + roadsapikey;
                 $.ajax({
                     url: googleurl,
                     dataType: 'json',
                     async: false, 
                     type: 'GET',
                     success: function (data) {

                             speeddata[i].speedlimits=data.speedLimits;

                     }

                 });
             }

         }

Updated the success function.

add comment if some problem

Edit: I have added async: false, to the call by that you will be able to fill speeddata object in it. Oter way is to make a callback by refactoring the code

8 Comments

Cannot set property 'speedlimits' of undefined
I will now post screen of response from google and update post
how this data object is like success: function (data) {... what is in data you are geeting from ajex call?
I paste screen shot in post.
can you access speeddata inside success?
|

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.