首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JavaScript数组foreach循环导致重复值

JavaScript数组foreach循环导致重复值
EN

Stack Overflow用户
提问于 2018-10-18 06:19:26
回答 1查看 484关注 0票数 0

我试图使用foreach循环将特定的对象添加到数组中,但是我的代码生成的数组有很多重复的值(在本例中为76),而结果应该是3。

然后使用结果为Google Maps Direction API添加路点,并将其显示在web页面上的Google Map中。

代码语言:javascript
复制
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionService = new google.maps.DirectionsService();
var map;
var mapOptions = {
  zoom: 14,

};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map)
var arr = [];
var beginpoint;
var endpoint;

$(document).ready(function() {
  $.getJSON("https://api.jsonbin.io/b/5bc7956651e8b664f2bcce19", function(data) {
    var json = []; // THE ARRAY TO STORE JSON ITEMS.
    $.each(data, function(index, value) {
      json.push(value); // PUSH THE VALUES INSIDE THE ARRAY.
      // Loop the JSON

      var arrayLength = json.length;
      for (var i = 0; i < arrayLength; i++) {
        if (json[i].GENRE == "CRIME" && json[i].CONTINENT == "NOORD-AMERIKA") {
          //console.log(json[i]); 
          latLng = new google.maps.LatLng(json[i].BREEDTEGRAAD, json[i].LENGTEGRAAD);
          arr.push({
            location: latLng,
            stopover: true
          });
        }

      }

    });
  });
});

function calculateRoute() {
  beginpoint = new google.maps.LatLng(arr[0].latitude, arr[0].longitude);
  endpoint = new google.maps.LatLng(arr[0].latitude, arr[0].longitude);

  console.log(arr);

  var request = {
    origin: beginpoint,
    destination: endpoint,
    waypoints: arr,
    optimizeWaypoints: true,
    travelMode: 'DRIVING'
  };

  directionService.route(request, function(result, status) {
    if (status == "OK") {
      directionsDisplay.setDirections(result);
    } else {
      console.log(result);
      console.log(status);
      alert("Er is iets fout gegaan");
    }
  });
}

document.getElementById('get').onclick = function() {
  calculateRoute();
}
代码语言:javascript
复制
#map-canvas {
  width: 500px;
  height: 500px;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB7deCJieASLxPQvF6qpX2xLi-mz6hKKSk&libraries=place" type="text/javascript"></script>
<div id="map-canvas"></div>
<button id="get">bereken</button>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-18 06:37:08

你不需要替身。您已经在一个与json中的对象相等的json循环值中。

试试这个:)

代码语言:javascript
复制
$(document).ready(function() {
  $.getJSON("https://api.jsonbin.io/b/5bc7956651e8b664f2bcce19", function(data) {
    $.each(data, function(index, value) {      
        if (value.GENRE == "CRIME" && value.CONTINENT == "NOORD-AMERIKA") {
          console.log(value); 
          latLng = new google.maps.LatLng(value.BREEDTEGRAAD, value.LENGTEGRAAD);
          arr.push({
            location: latLng,
            stopover: true
          });
        }
    });
  });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52864407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档