拆分数组给我不是一个函数

时间:2019-07-03 18:39:44

标签: javascript jquery

我有一个像这样的数组:

0: ["-22.91401497538739,-68.19866465000001"]
1: ["-25.857842171488155,-54.4140132"]
2: ["-33.39697196046993,-70.79329520000005"]
3: ["-34.61587176137625,-58.433298449999995"]
4: ["-34.92952451109041,138.59812584999997"]
5: ["14.841,-89.156"]

我需要拆分每个值,然后这样做:

for (var i = 0; i < coordsB.length; i++) {
  var latLng = new google.maps.LatLng(coordsB[i].split(','));

但是我得到了

Uncaught (in promise) TypeError: coordsB[i].split is not a function

完整代码:

    function externalContent() {
      fetch("https://www.example.com/wp-json/wp/v2/posts?per_page=50&status=publish", {
        headers: { 
          "Content-Type": "application/json",
        }
      }).then(function(response) { 
        return response.json(); 
      }).then(function(data) {
        coordsB = [];
        var externaldates = data.map(x=> x["data"]);
        externaldates = $.grep(externaldates, function(n){ return (n); });
        $.unique(externaldates.sort());
        $.each(data, function(i, item) {
          coordsB.push(data[i]["usp-custom-90"]["usp-custom-90"]);
        });
        coordsB = $.grep(coordsB, function(n){ return (n); });
        $.unique(coordsB.sort());
        for (var i = 0; i < coordsB.length; i++) {
          var latLng = new google.maps.LatLng(coordsB[i].split(','));
          var marker = new google.maps.Marker({
            position: latLng,
            linkToPost: externaldates[i]
          }),
          circle = new google.maps.Circle({
            radius: 1.1,
            fillColor: '#ff007f',
            strokeColor: '#ff007f',
            strokeOpacity: 0.75,
            strokeWeight: 20,
            linkToPost: links[i],
            longitude: longitude[i],
            latitude: latitude[i]
          });
          circle.bindTo('center', marker, 'position');
          circle.bindTo('map', marker, 'map');
          markers.push(marker);
          marker.setVisible(false);

          google.maps.event.addListener(circle, 'click', function() {
            $("#map").css("display", "none");
            $(".loader").css("display", "flex");
            $("body").css("background", "black");
            $(".flex-shrink-0").addClass("send");
            $("#longiTude").attr("value", this.longitude);
            $("#latiTude").attr("value", this.latitude);
            $("#timeSearch").attr("action", this.linkToPost);
            window.location.href = this.linkToPost;
          });
        }

      });
    }

1 个答案:

答案 0 :(得分:3)

您只需要调用coordsB[i][0].split(','),因为coordsB是列表列表