Turf.jsMeta

turf.coordReduce

What is turf.coordReduce?

turf.coordReduce walks every coordinate position in a GeoJSON object and folds them into a single accumulated value — the coordinate-level equivalent of Array.prototype.reduce.

JavaScript
turf.coordReduce(geojson, callback, initialValue?, excludeWrapCoord?) → any

The callback receives (previousValue, currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) and must return the next accumulator value.

When would you use turf.coordReduce?

Use turf.coordReduce when you need a scalar summary over all vertices — computing a bounding box manually, summing elevations stored in z-values, counting vertices that satisfy a predicate, or building an index keyed by feature.

It avoids the memory cost of coordAll-then-reduce on huge FeatureCollections because no intermediate flat array is created.

JavaScript
1const totalElevation = turf.coordReduce(
2  fc,
3  (sum, coord) => sum + (coord[2] ?? 0),
4  0
5);

FAQs

How do I install Turf.js to use this function?

Install npm install @turf/meta and import import { coordReduce } from '@turf/meta', or use turf.coordReduce from the umbrella @turf/turf bundle.

Is initialValue required?

No. If you omit it, the first coordinate is used as the initial accumulator and the callback starts from the second position — matching Array.prototype.reduce semantics.

Can I mutate the accumulator?

Yes, but prefer returning fresh values for pure-function style. If performance requires mutating (e.g. a growing array), mutate the accumulator and return it.

When should I prefer coordEach over coordReduce?

Use coordEach when you do not need an accumulated return — for example mutating coordinates in place or pushing to an external collector. Use coordReduce when the output is a single derived value whose construction follows reduce semantics.