I need a way to count the same property for each feature of a geojson file and obtain an array like this: array=["type 1", "type 2","type 3","type 2","type 1","type 2","type 1","type 3","type 1","type 1",....]
I'm loading a large geojson feature collection from a file.
This is not really a mapbox-gl problem. Your GeoJson is just standard JavaScript object, the features
is standard Array:
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" -> ...
)
Or even more concise:
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
}, {});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.