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
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.
-
Can you give a fiddle link and data object w/o the AJAX call?Pavan– Pavan2018年01月22日 14:32:26 +00:00Commented Jan 22, 2018 at 14:32
-
One second, I will post it@PavanYevhen– Yevhen2018年01月22日 14:33:00 +00:00Commented Jan 22, 2018 at 14:33
-
I cannot create fiddle, because it will have google roads api key. But I included speeddata data@PavanYevhen– Yevhen2018年01月22日 14:43:59 +00:00Commented Jan 22, 2018 at 14:43
-
And what do you want?Pavan– Pavan2018年01月22日 14:47:07 +00:00Commented 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 @PavanYevhen– Yevhen2018年01月22日 14:48:17 +00:00Commented Jan 22, 2018 at 14:48
2 Answers 2
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);
}
1 Comment
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 undefinedsuccess: function (data) {... what is in data you are geeting from ajex call?speeddata inside success?