Atlaslog
Atlaslog / Turf.js / API / clusterEach
Turf.js · API reference

clusterEach

Quick answer

Iterates over groups of features that share a property value, so each callback can compute a statistic for that group.

Last verified 2026-11-03 · Turf.js 7.x · Cite this page
Runnable examplesChinese noteAll API topics

Description​

clusterEach

Parameters​

NameTypeDescription
geojsonFeatureCollectionGeoJSON Features
propertystring | numberGeoJSON property key/value used to create clusters
callbackclusterEachCallbacka method that takes (cluster, clusterValue, currentIndex)

Returns​

void

Examples​

var geojson = turf.featureCollection([
turf.point([0, 0]),
turf.point([2, 4]),
turf.point([3, 6]),
turf.point([5, 1]),
turf.point([4, 2]),
]);

// Create a cluster using K-Means (adds `cluster` to GeoJSON properties)
var clustered = turf.clustersKmeans(geojson);

// Iterate over each cluster
turf.clusterEach(
clustered,
"cluster",
function (cluster, clusterValue, currentIndex) {
//= cluster
//= clusterValue
//= currentIndex
},
);

// Calculate the total number of clusters
var total = 0;
turf.clusterEach(clustered, "cluster", function () {
total++;
});

// Create an Array of all the values retrieved from the 'cluster' property
var values = [];
turf.clusterEach(clustered, "cluster", function (cluster, clusterValue) {
values.push(clusterValue);
});

Installation​

$ npm install @turf/clusters

import { clusterEach } from "@turf/clusters";
const result = clusterEach(...);
$ npm install @turf/turf

import * as turf from "@turf/turf";
const result = turf.clusterEach(...);
TutorialsAPI index
English reference text adapted from the official Turf.js documentation, © Turf.js contributors, licensed under MIT.
Original page: https://turfjs.org/docs/api/clusterEach · Chinese note: /turf/api/cluster-each.html
Cite this page

Atlaslog (2026). clusterEach. https://www.jingweizhi.com/en/turf/api/cluster-each.html (accessed 2026-11-03).