Last active
May 25, 2016 08:25
-
-
Save englishextra/872269c30d7cb2d10e3c3babdefc37b4 to your computer and use it in GitHub Desktop.
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
/*! | |
* return an array of values that match on a certain key | |
* techslides.com/how-to-parse-and-search-json-in-javascript | |
* gist.github.com/englishextra/872269c30d7cb2d10e3c3babdefc37b4 | |
* var jpr = JSON.parse(response); | |
* for (var i = 0; i < jpr.length; i++) { | |
* var o = jpr[i], | |
* t = getJsonKeyValues(o, "label"), | |
* p = getJsonKeyValues(o, "link"); | |
* crel(select, crel("option", { | |
* "value" : p, | |
* "title" : "" + t | |
* }, truncString("" + t, 37))); | |
* } | |
*/ | |
function getJsonKeyValues(obj, key) { | |
var objects = []; | |
for (var i in obj) { | |
if (!obj.hasOwnProperty(i)) | |
continue; | |
if (typeof obj[i] == "object") { | |
objects = objects.concat(getJsonKeyValues(obj[i], key)); | |
} else if (i == key) { | |
objects.push(obj[i]); | |
} | |
}; | |
return objects; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment