Get All Element In Array?

Asked by At

I have some stuck with turfJs,

I want to get all the elements of parent array, and set them into FeatureCollection I do iteration to all the array and get them isolated, but when passing them to FeatureCollection they take just the last one not all, so how to do that?

here is my Array "Users"

Array

and here is featureCollection

featureCollection

Function

 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 Answers

1
T.J. Crowder On

It's always a sign something's gone wrong if you use map on an array and you don't use the return value. The only purpose map serves is to go through an array and produce a new array of values created by mapping the old array's values to new values.

It sounds like you want an array of points, each point corresponding to an entry from providers. So map each provider to a point, and use the resulting array:

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

or with a concise arrow function:

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