Skip to content

Instantly share code, notes, and snippets.

@timwis
Created November 11, 2015 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timwis/dd7fdd857ae2cf9663a3 to your computer and use it in GitHub Desktop.
Save timwis/dd7fdd857ae2cf9663a3 to your computer and use it in GitHub Desktop.
Join 2 JSON objects on common property in JavaScript
$.when(
$.getJSON('https://data.phila.gov/resource/bbgf-pidf.geojson'),
$.getJSON('https://data.phila.gov/resource/r24g-zx3n.json?%24select=count(*)%20as%20value%2C%20%3A%40computed_region_bbgf_pidf%20as%20label&%24group=%3A%40computed_region_bbgf_pidf&%24order=value%20desc')
).done(function(responseGeojson, responseData) {
var data = responseData[0]
var geojson = responseGeojson[0]
// Create hash table for easy reference
var dataHash = {}
data.forEach(function(item) {
if(item.label) dataHash[item.label] = item.value
})
// Add value from hash table to geojson properties
geojson.features.forEach(function(item) {
item.properties.incidents = +dataHash[item.properties._feature_id] || null
})
console.log(geojson)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment