获取数组中的所有元素?

时间:2019-04-29 11:29:46

标签: javascript reactjs react-native geojson

我有些草皮了,

我想获取父数组的所有元素, 并将它们设置为 FeatureCollection 。我对所有数组进行迭代,并将其隔离, 但是当将它们传递给FeatureCollection时,它们仅取了最后一个而不是全部, 那么该怎么做呢?

这是我的数组“用户”

Array

这是featureCollection

featureCollection

功能

 handleNearby = () => {
        const { region, providers } = this.state;
        let points = [];
        providers.map((p, i) => {
            console.log(p.coordinates);
            console.log(i);
            points = turf.point([p.coordinates.latitude, p.coordinates.longitude])
            // console.log(points);
        });
        var collection = turf.featureCollection([points]);
        console.log(collection);
        console.log(providers);
        // var targetPoint = turf.point([region.longitude, region.latitude]);
        // var nearest = turf.nearestPoint(targetPoint, points);
        // var addToMap = [targetPoint, points, nearest];
    }

1 个答案:

答案 0 :(得分:1)

如果在数组上使用map并且不使用返回值,则总是有问题的迹象。 map的唯一用途是遍历一个数组并生成一个 new 值数组,该值是通过将旧数组的值映射到新值而创建的。

听起来您想要一个点数组,每个点对应于providers中的一个条目。因此,将每个提供程序映射到一个点,然后使用结果数组:

let points = providers.map(p => {
    return turf.point([p.coordinates.latitude, p.coordinates.longitude])
});
var collection = turf.featureCollection(points);
// Note no [] --------------------------^-----^

或带有简洁箭头功能:

let points = providers.map(p => turf.point([p.coordinates.latitude, p.coordinates.longitude]));
var collection = turf.featureCollection(points);