Así que tengo un objeto geoJSON que se ve así:
{"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]}}, .......}]Estoy buscando una función que consulte esto por una propiedad. Más específicamente, algo como:
getObjects(geoJsonObj,"ccamlr_id", ['48.1', '48.2']) o algo así, donde la consulta podría estar en múltiples objetos. En R, el equivalente sería algo así como filter(geJsonObj, 'ccamlr_id' %in% c('48.1', '48.2') . Estoy buscando un análogo de jQuery o Javascript.
Este enlace: use jQuery's find() en el objeto JSON estuvo bastante cerca, pero solo funciona en un solo valor. IE, getObjects(geoJsonObj, 'ccamlr_id', '48.1'); pero me gustaría obtenerlo para que funcione en múltiples valores de la clave.
La función getObjects es:
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; }eh, bueno, eso terminó siendo más fácil de lo esperado... un pequeño ajuste a la función anterior:
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 && $.inArray(obj[key], val) > -1) { objects.push(obj); } } return objects; }