gpt4 book ai didi

javascript - 使用 Mapbox GL JS 计算出现次数(geojson 属性)

转载 作者:行者123 更新时间:2023-12-03 02:15:29 27 4
gpt4 key购买 nike

我需要一种方法来计算 geojson 文件的每个特征的相同属性并获取如下数组: array=["type 1", "type 2","type 3","type 2","类型 1","类型 2","类型 1","类型 3","类型 1","类型 1",....]

我正在从文件加载大型 geojson 特征集合。

最佳答案

这并不是一个真正的mapbox-gl 问题。你的 GeoJson 只是标准的 JavaScript 对象,features 是标准数组:

const counts = new Map();

for (const feature of geojson.feature) {
const alert = feature.properties.alert;

if (!alert) {
continue;
}

if (!counts.has(alert)) {
counts.set(alert, 0);
}

const currentCount = counts.get(alert);
counts.set(alert, currentCount + 1);
}

// counts will look like this
Map(
"type1" -> 10,
"type2" -> 8,
"type3" -> ...
)

或者更简洁:

const counts = geojson.features.reduce((accumulatedCounts, feature) => {
const alert = feature.properties.alert;

if (!alert) return accumulatedCounts;
if (!accumulatedCounts[alert]) accumulatedCounts[alert] = 0;

accumulatedCounts[alert]++;

return accumulatedCounts
}, {});

关于javascript - 使用 Mapbox GL JS 计算出现次数(geojson 属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49384842/

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