Adding to GeoJSON Properties

I have a dataset and the IDs that connect to GeoJSON by state. I’m wondering how I can merge the two together so I have a GeoJSON that now includes more properties for each feature. Basically, I want to expand the stateShapes so that it has properties for each of the features, using my dataset to join against, but I’m having trouble figuring out how I could join it up. How can I wrangle my data so that each feature joins by their id and now has more properties?

Notebook

Always learning,
Zach

1 Like

I’ve sent you a suggestion, using a Map to index the data, then reading the values from that map for each of the features.

1 Like

Cool that’s great! And I could have the Map index to multiple values, like if I wanted to copy over the state name as well, plus other data?

I basically want to join by “id”, but put all of the values in the properties field, extending it.

Thank you for this discussion. @Fil - I see that the new data is nested under a field with that name. I have been wondering if it’s possible to accomplish this without nesting… For example, in this case matching state name to state name, then just writing in the additional attributes at that same property level?

yes, exactly. With the new index you could do a merge between the existing and the new properties, like so:

f.properties= ({
  ...f.properties,
  ...index1.get(f.properties.name)
});

You could also use d3.index(data, d => d.name) as a shorthand for new Map(data, d => [d.name, d])

2 Likes

Thanks Fil and Zach!
I re-read Zach’s notebook and Fil’s tips here, but I am still a bit confused about why things are working like they are. I forked Zach’s notebook and added a few comments/questions… in case you have another few moments to help? <3

Thank you for this opportunity to learn!