Created
July 20, 2020 09:30
-
-
Save damianwajer/72915f41ca609ed291dfcd55dc292b8d to your computer and use it in GitHub Desktop.
[JavaScript] Find deep data in an object.
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
/** | |
* Find deep data in an object. | |
* | |
* @param {object} data | |
* @param {string} path | |
* @return {*} | |
*/ | |
function findDeepData(data, path) { | |
const keysArr = path.split("."); | |
for (let i = 0; i < keysArr.length; ++i) { | |
if (typeof data[keysArr[i]] === "undefined") { | |
throw new Error("Wrong path: " + path); | |
} else { | |
data = data[keysArr[i]]; | |
} | |
} | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment