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"


and here is featureCollection



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

1 Answers

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 = => {
    return turf.point([p.coordinates.latitude, p.coordinates.longitude])
var collection = turf.featureCollection(points);
// Note no [] --------------------------^-----^

or with a concise arrow function:

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