Javascript 使用Mapbox GL JS(geojson属性)统计发生率

Javascript 使用Mapbox GL JS(geojson属性)统计发生率,javascript,mapbox,mapbox-gl-js,Javascript,Mapbox,Mapbox Gl Js,我需要一种方法来计算geojson文件的每个特性的相同属性,并获得如下数组:array=[类型1,类型2,类型3,类型2,类型1,类型3,类型1,…] 我正在从一个文件加载一个大型geojson功能集合。这实际上不是mapbox gl问题。您的GeoJson只是标准JavaScript对象,其功能是标准数组: const counts = new Map(); for (const feature of geojson.feature) { const alert = feature.pr

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


我正在从一个文件加载一个大型geojson功能集合。

这实际上不是mapbox gl问题。您的GeoJson只是标准JavaScript对象,其功能是标准数组:

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
}, {});
我希望此演示有助于: