【问题标题】:Use Foreach Loop in variable在变量中使用 Foreach 循环
【发布时间】:2016-11-05 12:55:38
【问题描述】:

我正在使用谷歌地图进行实时跟踪。我必须根据数据库更新更改标记。

这是一个 testLocs 变量。我想让它动态化。

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); 

因此,对于每 3000(3 秒)从 ajax 调用一次,我调用 ajax 脚本。 所以,我得到了这种格式的 ajax 响应。

在控制台中,我在 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

现在,我想在上面的 var testLocs 中使用它。

整个函数,

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);

我试过了,但它不起作用。

var testLocs = {

      new_latitude.forEach(function(single_value)
         {
            single_value_latitude = single_value;
            // alert(single_value_latitude);
         });
}

编辑:

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);
            }
    });
})();

我在 ajax 响应后得到下面的数组,console.log(data);

数组:

包含 4 个对象的数组,在每个对象中我得到了所有信息 device_id、device_name、纬度、经度。 在控制台中,我在此信息中得到了数组。

 [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"}]

【问题讨论】:

  • 您从 AJAX 请求中得到的确切响应是什么?您当前的 3 个数组列表是无效的语法。另外,您是否可以更改响应?让它以你期望的格式返回比之后在 JS 中破解它要容易得多
  • @RoryMcCrossan。感谢您的答复。我从数据库中获得了 ajax 响应中的整个数组。但是 var testLocs 的格式不同,所以我认为如果我使用 lat、long 等来划分它,那么它可能是可能的。我正在用整个数组更新我的问题。和 ajax 代码。
  • @Bhavin 收到的对象中没有“名称”属性。因此,每个回调中的 "var namei=v['name']" 是必须失败的行。您可以仔细检查值或代码吗?
  • @PratikGaikwad。谢谢你指出。是我错误地在数组中得到了名字。
  • @Bhavin 你的问题解决了吗?还是仍然存在?

标签: javascript php jquery ajax


【解决方案1】:

转换代码(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);

所以,您的函数可能如下所示:

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);
}

【讨论】:

  • 感谢您的回复。我收到此错误:无法读取 undefined(...) 的属性 'reduce'。
  • 运行良好,非常感谢您提供此解决方案。我仍然收到此错误无法读取 undefined(...) 的属性 'reduce'。
  • new_name 应该是您的姓名数组(["Device-1", "Device-2", "Device-3"]),console.log(new_name )?如果没有,那么您可能没有在您的函数中收到它
  • 哦,知道了,现在可以正常工作了。感谢您提供此解决方案:)
猜你喜欢
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-17
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
相关资源
最近更新 更多