🏠 back to Observable

returning all found instances, or rendering a group [using d3.array@^2.2]

I have a .geojson dateset of sub-district administrative divisions. I would like to map out all the administrative divisions within a given district, but I can’t assemble them correctly.

I have tried using array.find:

ghimire_admin3_DANG_find = ghimire_admin3.features.find(f => f.properties.DISTRICT === "DANG")

But as the documentation notes:

The value of the first element in the array that satisfies the provided testing function.

Since I have several instances of in the data, and want to get them all, I tried using d3.group:

ghimire_admin3_DANG_group = d3.group(ghimire_admin3.features, f => f.properties.DISTRICT === "DANG")

But the resulting map would render as is; it needs to be narrowed to the resulting group of objects identified as ‘true’.

Any tips for how to get at this group of 10 ‘true’ values and to map them? The map in the notebook is derived entirely from Mike’s GeoJSON Viewer [I would have forked, but started the notebook just playing w/ the data on my own.]

Here’s my progress:

… Probably I am going about this the wrong way and should be using reduce and filter.

Try array.filter.

ghimire_admin3_DANG = ghimire_admin3.features.filter(f => f.properties.DISTRICT === "DANG")

These features can then be rendered individually or combined into a FeatureCollection.

({type: "FeatureCollection", features: ghimire_admin3.features.filter(f => f.properties.DISTRICT === "DANG")})

You can also use topojson.merge to combine them into a single poygon, but only if you convert to TopoJSON first.

1 Like

Thanks Mike!

These features can then be rendered individually or combined into a FeatureCollection.

That appears to be the trick: combining the returned array into a Feature Collection! Super helpful, as I’ve struggled a lot with this in the past and had been loading in individual array elements… painfully… before.

Thanks again!