Created
October 19, 2021 09:26
-
-
Save osmancansiz/e4a53611c830b989002d01b153bdaa94 to your computer and use it in GitHub Desktop.
Deep Nested Objects Search
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
deepSearch(object, key, predicate) { | |
if (object.hasOwnProperty(key) && predicate(key, object[key]) === true) | |
return object; | |
for (let i = 0; i < Object.keys(object).length; i++) { | |
let value = object[Object.keys(object)[i]]; | |
if (typeof value === "object" && value != null) { | |
let o = this.deepSearch(object[Object.keys(object)[i]], key, predicate); | |
if (o != null) return o; | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment