【问题标题】:Get All Element In Array?获取数组中的所有元素?
【发布时间】:2019-04-29 11:29:45
【问题描述】:

我有一些被 turfJs 卡住了,

我想获取父数组的所有元素, 并将它们设置为 FeatureCollection 我对所有数组进行迭代并将它们隔离, 但是当将它们传递给 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];
    }

【问题讨论】:

标签: javascript reactjs react-native geojson


【解决方案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);

【讨论】:

猜你喜欢
  • 2011-04-30
  • 2020-09-29
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多