Turf.jsMeta

turf.featureReduce

What is turf.featureReduce?

turf.featureReduce folds every Feature of a GeoJSON input into a single accumulated value. It works on a FeatureCollection, a single Feature, or a bare Geometry.

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

The callback receives (previousValue, currentFeature, featureIndex).

When would you use turf.featureReduce?

Use turf.featureReduce for any summary that requires visiting every feature — total area across a FeatureCollection, count of features matching a predicate, a Map keyed by one property, or a running running accumulator in a streaming pipeline.

It gives you the same ergonomics as Array.prototype.reduce while handling the edge cases of single Feature / bare Geometry inputs, which is useful for writing helpers that accept any GeoJSON shape.

JavaScript
1const totalArea = turf.featureReduce(
2  parcels,
3  (sum, feature) => sum + turf.area(feature),
4  0
5);

FAQs

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

Install npm install @turf/meta and import import { featureReduce } from '@turf/meta', or via the @turf/turf umbrella as turf.featureReduce.

What happens if initialValue is omitted?

The first feature becomes the initial accumulator and iteration starts from the second — identical to Array.prototype.reduce. For an empty input with no initial value, the function returns undefined.

Can I return any accumulator type?

Yes — the accumulator can be a number, object, Map, or anything. Just be consistent about what your callback returns each iteration.

Should I use featureReduce or featureEach + external variable?

Both work. featureReduce is more functional and avoids closure over a mutable outer variable; featureEach with a push or += is fine for small scripts. For production code, reduce is clearer about intent.