Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

javascript - 不清楚为什么在尝试向数组添加JSON功能时得到奇怪的结果(Unclear why I'm getting a strange result when trying to add JSON features to an array)

I'm working on a function in leaflet that changes the style weight of the most recent object in a JSON response.(我正在使用传单中的函数来更改JSON响应中最新对象的样式weight 。)

That's beside the point though, because I can't properly get the JSON objects into an array.(不过,这很重要,因为我无法正确地将JSON对象放入数组中。)

When I run console.log(dates) in order to see if they were pushed to the dates array I return this in the console.(当我运行console.log(dates)以便查看它们是否被推送到dates数组时,我在控制台中将其返回。)

A bunch of empty arrays, amounting to the number of dates in the JSON response.(一堆空数组,总计为JSON响应中的日期数。)

在此处输入图片说明

When I run console.log(time) to ensure I am in fact reaching the correct json feature I receive the dates (epoch format) as expected, but can not understand why they will not be pushed into the array dates .(当我运行console.log(time)以确保我实际上达到了正确的json功能时,我收到了预期的日期(纪元格式),但无法理解为什么它们不会被推入数组dates 。)

Any ideas?(有任何想法吗?)

在此处输入图片说明

Function(功能)

//most recent earthquake identifer

function mostRecent(time) {
    var dates=[];
    for (var i = 0; i < time.length; i++) {
        dates.push(time[i])
    }
    console.log(dates)
return true
}

Javascript(Java脚本)

// adds geojson feed of earthquakes from USGS url (must create a function to layer it on leaflet)

$.getJSON('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson', function(earthQuakes) {
    var points = L.geoJSON(earthQuakes, {
        filter: eqFilter,
        onEachFeature: function (feature, layer) {  // binds data in geosjon to a popup
            var eqDate = new Date(feature.properties.time); // converts epoch date
            layer.bindPopup(
                '<b>Location: </b>' + feature.properties.place + '<br>' +
                '<b>Magnitude: </b>' + feature.properties.mag + '<br>' +
                '<b>Depth: </b>' + feature.geometry.coordinates[2] + 'km' + '<br>' +
                '<b>Time: </b>' + eqDate.toGMTString() + '<br>' +
                '<br><center><a href=' + feature.properties.url + '>USGS Details</a></center>',
            )
        },
        pointToLayer: function(feature, latlng){  // changes default icons to circles and styles accordingly
            return new L.CircleMarker(latlng, {
                radius: circleSize(feature.properties.mag),
                fillColor: getColor(feature.properties.mag),
                color: "#000",
                weight: mostRecent(feature.properties.time),
                opacity: 1,
                fillOpacity: 0.5,
            });

        }
    }).addTo(map);
    map.fitBounds(points.getBounds()); // pans to points
});
  ask by prime90 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Make the dates variable a global variable.(将dates变量设为全局变量。)

You're actually resetting the dates array on every function call.(您实际上是在每个函数调用上重置dates数组。) Refactor it to something like this(重构成这样的东西)
var dates = [];

function mostRecent(time) {
  dates.push(time);
  console.log(dates);
  return true
}

It should be working.(它应该正在工作。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...