Skip to content

Instantly share code, notes, and snippets.

@Artistan
Last active April 3, 2016 23:54
Show Gist options
  • Save Artistan/1d2d62f46b64724d088a0d47035076dc to your computer and use it in GitHub Desktop.
Save Artistan/1d2d62f46b64724d088a0d47035076dc to your computer and use it in GitHub Desktop.
Find properties within a javascript object/array
function recursiveIterationKey(object,matchKey) {
var found=false;
for (var property in object) {
if (object.hasOwnProperty(property)) {
if (typeof object[property] == "object"){
if(recursiveIterationKey(object[property])){
found=true;
break;
} else if(matchKey==property) {
found = true;
}
}else{
//found a property which is not an object, check for your conditions here
if(matchKey==property){
found = true;
}
}
}
}
return found;
}
function recursiveIterationValue(object,matchValue) {
var found=false;
for (var property in object) {
if (object.hasOwnProperty(property)) {
if (typeof object[property] == "object"){
if(recursiveIterationValue(object[property])){
found=true;
break;
}
}else{
//found a property which is not an object, check for your conditions here
if(matchValue==object[property]){
found = true;
}
}
}
}
return found;
}
function recursiveIterationKeyValue(object,matchKey,matchValue) {
var found=false;
for (var property in object) {
if (object.hasOwnProperty(property)) {
if (typeof object[property] == "object"){
if(recursiveIterationKeyValue(object[property])){
found=true;
break;
} else if(matchKey==property && matchValue==object[property]) {
found = true;
}
}else{
//found a property which is not an object, check for your conditions here
if(matchKey==property && matchValue==object[property]){
found = true;
}
}
}
}
return found;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment