So I have a geoJSON object that looks like this:
{"type": "FeatureCollection", "features":
[{"type": "Feature",
"properties": {
"site_name": "Active Reef (Active Sound)",
"ccamlr_id": "48.1",
"site_id": "ACTI",
"model": "penguinmap.site"},
"id": "ACTI",
"geometry": {
"type": "Point",
"coordinates": [-2435551.681071794, 1650542.7689355933]
}},
{"type": "Feature",
"properties": {
"site_name": "Acuna Island",
"ccamlr_id": "48.2",
"site_id": "ACUN",
"model": "penguinmap.site"},
"id": "ACUN",
"geometry": {
"type": "Point",
"coordinates": [-2279902.543759384, 2308975.9799890867]}},
.......}]
I'm looking for a function that queries this by a property. More specifically, something like:
getObjects(geoJsonObj,"ccamlr_id", ['48.1', '48.2'])
or something like that, where the query could be on multiple objects. In R the equivalent would be something like filter(geJsonObj, 'ccamlr_id' %in% c('48.1', '48.2')
. I'm looking for a jQuery or Javascript analog.
This link:
use jQuery's find() on JSON object was pretty close, but it only works on a single value. I.E.,
getObjects(geoJsonObj, 'ccamlr_id', '48.1');
but I'd like to get it so it works on multiple values of the key.
The getObjects function is:
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else if (i == key && obj[key] == val) {
objects.push(obj);
}
}
return objects;
}