Created
November 11, 2015 17:31
-
-
Save timwis/dd7fdd857ae2cf9663a3 to your computer and use it in GitHub Desktop.
Join 2 JSON objects on common property in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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