[英]Use Foreach Loop in variable
I'm working on google map for live tracking. 我正在使用谷歌地图进行实时跟踪。 i have to change markers according to database Updation. 我必须根据数据库更新更改标记。
This is a testLocs variable. 这是一个testLocs变量。 i want to make it dynamic. 我想让它充满活力。
var testLocs = {
1: { info:'Demo', lat:31.933517, lng:74.910278 },
2: { info:'Demo', lat:32.073266 , lng:76.997681 },
3: { info:'Demo', lat:32.23139 , lng:78.425903 },
}
setMarkers(testlocs);
So, For that from ajax call every 3000 (3 seconds) i call ajax script. 所以,对于那个从ajax调用每3000(3秒)我调用ajax脚本。 So, I got response from ajax in this format. 所以,我得到了这种格式的ajax的回复。
In Console i got 3 Arrays from database in ajax response, 在Console中,我在ajax响应中从数据库中获得了3个数组,
Array-1 : ["Device-1", "Device-2", "Device-3"]; // all device name
Array-2 : ["31.933517", "32.073266", "32.23139"]; // all latitude
Array-3 : ["74.910278", "76.997681", "78.425903"]; // all longitude
Now,I want to use it in Above var testLocs. 现在,我想在Above var testLocs中使用它。
Whole Function, 整体功能,
function myTimer(new_latitude, new_longitude,new_name)
{
var new_latitude = new_latitude;
var new_name = new_name;
var new_longitude = new_longitude;
console.log(new_name);
console.log(new_latitude);
console.log(new_longitude);
var testLocs = {
1: { info:'Demo', lat:31.933517, lng:74.910278 },
2: { info:'Demo', lat:32.073266 , lng:76.997681 },
3: { info:'Demo', lat:32.23139 , lng:78.425903 },
}
setMarkers(testlocs);
}
var myVar = setInterval(function(){ myTimer() }, 3000);
I tried with this but its not working. 我试过这个,但它不起作用。
var testLocs = {
new_latitude.forEach(function(single_value)
{
single_value_latitude = single_value;
// alert(single_value_latitude);
});
}
EDIT : 编辑:
Ajax code Ajax代码
(function worker() {
$.ajax({
type: "POST",
dataType: "json",
// data: {business1: business1},
url: '<?php echo site_url('home/automatic_fetch_data'); ?>',
success: function (data) {
//alert(data);
var new_lat = [];
var new_lng = [];
var new_name = [];
$.each(data, function(k, v) {
var lati = v['latitude'];
var lngi = v['longitude'];
var namei = v['name'];
new_lat.push(lati);
new_lng.push(lngi);
new_name.push(namei);
});
var new_latitude = new_lat;
var new_longitude = new_lng;
var new_name = new_name;
// console.log(new_latitude);
// console.log(new_longitude);
myTimer(new_latitude, new_longitude,new_name);
},complete: function() {
// Schedule the next request when the current one's complete
setTimeout(worker, 3000);
}
});
})();
I got below array in after ajax response, console.log(data)
; 我在ajax响应, console.log(data)
之后得到了数组;
Array : 数组:
Array with 4 Object and in each object i got all the information device_id, device_name, latitude, longitude. 带有4个对象的数组,在每个对象中,我获得了所有信息device_id,device_name,纬度,经度。 In Console i got array in this information. 在Console中我得到了这个信息中的数组。
[Object { id="1", device_id="1", device_name="Device-1", latitude="29.630771", longitude="74.910278"}, Object { id="2", device_id="2", device_name="Device-2", latitude="32.073266", longitude="76.997681"}, Object { id="3", device_id="5", device_name="Device-3", latitude="29.630771", longitude="74.910278"}, Object { id="5", device_id="3", device_name="Device-3", latitude="29.630771", longitude="74.910278"}]
transformation code (3 arrays into object with another 3 objects): 转换代码(3个数组到另外3个对象的对象):
var new_name = ["Device-1", "Device-2", "Device-3"]; var new_latitude = ["31.933517", "32.073266", "32.23139"]; // all latitude var new_longitude = ["74.910278", "76.997681", "78.425903"]; var testLocs = new_name.reduce(function (res, curr, currentIndex) { res[currentIndex + 1] = { info: new_name[currentIndex], lat: new_latitude[currentIndex], lng: new_longitude[currentIndex] }; return res; }, {}); console.log(testLocs);
so, your function may look like: 所以,你的功能可能如下所示:
function myTimer(new_latitude, new_longitude,new_name) {
console.log(new_name);
console.log(new_latitude);
console.log(new_longitude);
var testLocs = new_name.reduce(function (res, curr, currentIndex) {
res[currentIndex + 1] = {
info: new_name[currentIndex],
lat: new_latitude[currentIndex],
lng: new_longitude[currentIndex]
};
return res;
}, {});
setMarkers(testlocs);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.