turf.featureEach
What is turf.featureEach?
turf.featureEach iterates over every Feature in a GeoJSON object, calling a callback for each one. It accepts a single Feature, a FeatureCollection, or a Geometry (treated as a single unnamed feature).
turf.featureEach(geojson, callback) → voidThe callback is invoked with (currentFeature, featureIndex).
When would you use turf.featureEach?
Use turf.featureEach when you want forEach-style iteration that works on both a single feature and a FeatureCollection without special-casing the input type. It is also the usual choice for side effects — mutating properties, dispatching render events per feature, or logging.
Because it is structural rather than coordinate-level, it pairs naturally with spatial predicates you apply inside the callback. For per-vertex work, reach for coordEach instead.
1turf.featureEach(fc, (feature, i) => {
2 feature.properties.index = i;
3 feature.properties.area = turf.area(feature);
4});FAQs
How do I install Turf.js to use this function?
Install npm install @turf/meta and import import { featureEach } from '@turf/meta', or use turf.featureEach via the @turf/turf bundle.
Does featureEach return a new FeatureCollection?
No. It iterates in place and returns undefined. To build a new collection, push into an array inside the callback or use turf.featureReduce with an accumulator.
Can I early-exit the iteration?
Turf's featureEach does not support a return-false abort. If you need to stop early, throw a sentinel and catch it outside, or use a plain for loop over features.
When should I use featureEach vs geomEach?
featureEach hands you the whole Feature (geometry plus properties). turf.geomEach gives you only the geometry, and for multi-geometries it breaks them into their primitives. Choose based on whether you need properties or pure geometries.