MEASUREMENT
COORDINATE MUTATION
TRANSFORMATION
FEATURE_CONVERSION
MISC
HELPER
RANDOM
GRIDS
AGGREGATION
META
ASSERTIONS
BOOLEANS
UNIT CONVERSION
DATA
JOINS
CLASSIFICATION

featureEach

简介

featureEach 是一个Turf.js库中的函数,它能够对GeoJSON要素中的每个要素进行迭代,并将其作为参数传递给回调函数。该函数可以用于对要素进行处理、修改和过滤等操作。

语法

turf.featureEach(features, callback)

参数

  • featuresFeature | FeatureCollection类型的GeoJSON要素,包括各种几何类型的地理要素
  • callback:回调函数,该函数将被应用于GeoJSON要素的每个要素

用法示例

const points = turf.featureCollection([
  turf.point([0, 0]),
  turf.point([10, 10]),
  turf.point([20, 20])
]);

turf.featureEach(points, (currentFeature, featureIndex) => {
  console.log(`Feature ${featureIndex}:`, currentFeature);
});

输出结果:

Feature 0: {"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[0,0]}}
Feature 1: {"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[10,10]}}
Feature 2: {"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[20,20]}}

注意事项

  • 回调函数必须接受两个参数:当前要素和当前要素的索引
  • 此函数不会在原始GeoJSON对象上进行修改,而是创建并返回新的GeoJSON对象。如果需要在原始对象上进行操作,应自己进行拷贝操作

参考文献