gpt4 book ai didi

javascript - 不清楚为什么在尝试将 JSON 功能添加到数组时会得到奇怪的结果

转载 作者:行者123 更新时间:2023-12-02 22:25:14 25 4
gpt4 key购买 nike

我正在传单中开发一个函数,该函数可以更改 JSON 响应中最新对象的样式 weight。但这不是重点,因为我无法正确地将 JSON 对象放入数组中。

当我运行 console.log(dates) 以查看它们是否被推送到 dates 数组时,我在控制台中返回它。一堆空数组,相当于 JSON 响应中的日期数。

enter image description here

当我运行 console.log(time) 以确保我实际上达到了正确的 json 功能时,我按预期收到了日期(纪元格式),但无法理解为什么它们不会插入数组日期。有什么想法吗?

enter image description here

函数

//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

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

最佳答案

使dates 变量成为全局变量。您实际上是在每次函数调用时重置dates 数组。将其重构为这样的内容

var dates = [];

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

它应该可以工作。

关于javascript - 不清楚为什么在尝试将 JSON 功能添加到数组时会得到奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59094194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com