turf.coordEach
What is turf.coordEach?
turf.coordEach traverses every coordinate position in a GeoJSON object — Feature, FeatureCollection, or Geometry — and calls a callback with the position and a set of structural indices. It is the canonical way to read or mutate coordinates in place.
turf.coordEach(geojson, callback, excludeWrapCoord?) → voidThe callback is invoked with (currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex). If excludeWrapCoord is true, the closing vertex of each polygon ring is skipped.
When would you use turf.coordEach?
Reach for turf.coordEach whenever you need to operate on every vertex — rounding coordinates to a precision, shifting coordinates by an offset, validating each position, or mutating them in place (for example snapping to a grid before writing to disk). Because it mutates the original object, no new FeatureCollection is returned — operate carefully if you might need the source later.
It is also the correct tool when you need the position's structural context: the callback receives the feature index, the multi-geometry index, and the geometry index, letting you group vertices by their owning feature.
1turf.coordEach(fc, (coord) => {
2 coord[0] = Math.round(coord[0] * 1e6) / 1e6;
3 coord[1] = Math.round(coord[1] * 1e6) / 1e6;
4});FAQs
How do I install Turf.js to use this function?
Install npm install @turf/meta and import with import { coordEach } from '@turf/meta', or use turf.coordEach via the @turf/turf bundle.
Does coordEach return a new feature?
No. It iterates in place and returns nothing. If your callback mutates the position array, those changes persist on the original GeoJSON. Clone first with turf.clone if you need an immutable copy.
What does excludeWrapCoord do?
Polygon rings close by repeating the first coordinate as the last. Passing true skips that closing vertex so you do not double-count it — useful when summing or averaging coordinates over a ring.
When should I use coordEach vs coordAll?
Use coordEach when you need indices, want to mutate in place, or want to short-circuit (throw inside the callback). Use coordAll when you just need the flat list of positions for a quick computation.