propEach函数可以在GeoJSON对象中的每个要素上按照给定参数操作它们的属性。例如,将所有要素的属性添加一个前缀或后缀。
geojson (FeatureCollection|Feature|Geometry):要素集合或单个要素。callback (Function):对每个要素进行操作的回调函数,该函数应该接受两个参数:要素和索引。回调函数返回一个新的属性对象,或者直接修改原始属性对象。options (Object, 可选):额外的选项。feature (Feature):当前要素。index (Number):当前要素在要素集合中的索引。mutate (Boolean, 默认值:false):在修改原始要素之前创建一个副本。如果设置为true,则不会创建副本,直接修改原始要素对象。geojson参数相同的对象,但已经通过回调函数更改了每个要素的属性。import { propEach } from '@turf/turf'
const points = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
properties: { id: 1, name: 'Point 1' },
geometry: { type: 'Point', coordinates: [0, 0] }
},
{
type: 'Feature',
properties: { id: 2, name: 'Point 2' },
geometry: { type: 'Point', coordinates: [0, 10] }
}
]
}
// 添加前缀
const prefixedPoints = propEach(points, (feature) => {
return {
...feature.properties,
name: `Prefixed ${feature.properties.name}`
}
})
// 添加后缀
const suffixedPoints = propEach(points, (feature) => {
return {
...feature.properties,
name: `${feature.properties.name} Suffix`
}
})
mutate选项设置为true时,函数将修改原始GeoJSON对象,此行为可能不可撤销,请谨慎使用。