turf.geomEach
What is turf.geomEach?
turf.geomEach walks every Geometry in a GeoJSON object, yielding each one to a callback. Unlike flattenEach, it preserves Multi-variants as a single iteration rather than breaking them into parts.
turf.geomEach(geojson, callback) → voidThe callback receives (currentGeometry, featureIndex, featureProperties, featureBBox, featureId).
When would you use turf.geomEach?
Use turf.geomEach when you need to inspect or mutate geometries while keeping access to the owning Feature's properties and id. It is the right tool for per-geometry transformations that depend on metadata — for example a coordinate-system conversion that reads a projection property from the parent Feature.
It is also the fastest way to walk a mixed FeatureCollection whose Features may be bare Geometries or Features with a GeometryCollection — geomEach handles all those cases uniformly.
1turf.geomEach(fc, (geom, i, props) => {
2 if (props?.type === 'restricted') geom.coordinates = null;
3});FAQs
How do I install Turf.js to use this function?
Install npm install @turf/meta and import import { geomEach } from '@turf/meta', or reach turf.geomEach through @turf/turf.
Does geomEach split MultiPolygons into their parts?
No. A MultiPolygon is yielded once as a single geometry. If you want one callback per part, use flattenEach.
Does it walk into GeometryCollections?
Yes. The geometries inside a GeometryCollection are each yielded to the callback — with the parent Feature's properties passed along so you can associate them.
Can I mutate the geometry inside the callback?
Yes, geomEach references the original geometry object. Mutations persist on the source GeoJSON. Clone with turf.clone first if you need an untouched copy.