我对JS和堆栈社区也很陌生,我很难从我的本地json文件中获取符合条件的值。原来的json非常大,所以这些是我的json的前两行;
[{
"park_name": "Acadia National Park",
"latitude": 44.35,
"longitude": -68.21,
"species_id": "ACAD-1000",
"common_names": "Moose",
"occurrence": "Present",
"abundance": "Rare",
"nativeness": "Native",
"conservation_status": null,
"location": "44.35, -68.21"
},
{
"park_name": "Acadia National Park",
"latitude": 44.35,
"longitude": -68.21,
"species_id": "ACAD-1001",
"common_names": "Northern White-Tailed Deer, Virginia Deer, White-Tailed Deer",
"occurrence": "Present",
"abundance": "Abundant",
"nativeness": "Native",
"conservation_status": null,
"location": "44.35, -68.21"
}
]
我正在尝试使用渐变点创建传单地图。 [物种越适合条件,点越大]。我的第一层是“必须看到公园”我想得到的物种是occurrence = "Present"
和abundance = "Rare"
(这存在于此处未显示的其他行中)并根据它们的位置将点放在地图上。每个公园都有许多适合这种情况的物种,例如在“阿卡迪亚国家公园”中,141种物种符合条件,所以我只需要根据适合的物种数量分级一个点。我可以获得物种,但不能抓住包含141种的一个公园的纬度和经度,而对于其他公园它是相同的,我有一堆物种,但只需要一个[lat, lon]
。这是我获取条件的代码;
var visit = {};
var visit_loc = {};
for (var i = 0; i < metadata.length; i++) {
var names = metadata[i].park_name;
var abundance = metadata[i].abundance;
var occurrence = metadata[i].occurrence;
var locations = metadata[i].location;
var latitude = metadata[i].latitude;
var longitude = metadata[i].longitude;
if (occurrence === "Present" && abundance === "Rare") {
if (!visit[names] && !visit_loc[location]) {
visit[names] = 1;
visit_loc[location] = 1;
} else {
visit[names]++;
visit_loc[location]++;
}
}
}
这里的元数据是我对当地json的回复。我从这个公式得到的输出是这样的:
Acadia National Park: 141
Arches National Park: NaN
Badlands National Park: NaN
Big Bend National Park: NaN
Biscayne National Park: NaN
...
而对于visit_loc
,输出是这样的:
visit_loc
{http://localhost:8000/: 10768}
http://localhost:8000/: 10768
__proto__:
constructor: ƒ Object()
hasOwnProperty: ƒ hasOwnProperty()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toLocaleString: ƒ toLocaleString()
toString: ƒ toString()
valueOf: ƒ valueOf()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()
我不知道这意味着什么。
所以在这里,第一个之后的公园给我NaN,但在我将代码更改为:
if (occurrence === "Present" && abundance === "Rare") {
if(!visit[names]){
visit[names] = 1;
} else{
visit[names]++;
}
我得到输出,每个公园的值,输出转为:
Acadia National Park: 141
Arches National Park: 132
Badlands National Park: 38
Big Bend National Park: 154
Biscayne National Park: 389
哪个好,我得到了我想要的值,但我无法得到符合标准的公园的纬度和经度值。
很抱歉让它保持这么久并且变得复杂,这是我在这里问的第一个问题,是否有人知道如何仅为每个公园获取一次纬度和经度值。谢谢。
你需要独立增加visit
和visit_loc
。当两个元素尚未设置时,您的代码仅将元素设置为1
。如果未设置visit[names]
,但设置了visit[locations]
,则代码假定两者都已初始化并尝试将它们两者递增。 visit[names]
失败,所以你在那里得到NaN
。
if (occurrence === "Present" && abundance === "Rare") {
if(!visit[names]){
visit[names] = 1;
} else{
visit[names]++;
}
if (!visit_loc[locations]) {
visit_loc[locations] = 1;
} else {
visit_loc[locations]++;
}
}
首先,我认为您的数据集结构错误。您不能将公园数据与物种数据混合。这是您首先遇到此问题的根本原因。还要检查访问元素的方式,它可以比使用for循环更简单。无论如何,让我们把它放在一边,我会跳进去尝试解决问题的最后一个缺失部分。
假设您拥有符合条件的公园名称,您只需在对象数组中搜索其名称并返回其坐标:
function getParkLocationByName(parkName) {
let {latitude, longitude} = data.find(park => park.park_name === parkName);
return {latitude, longitude};
}
let coords = getParkLocationByName('Acadia National Park');
console.log(coords)
请注意,该段代码中的“数据”是您原来的json。