Created
January 19, 2023 21:23
-
-
Save Slackwise/3f7c3d65d56bcbb64c167454ec3c5154 to your computer and use it in GitHub Desktop.
Recursively navigates into nested collections inside `collection` by following a sequence of properties `pathArray`, executing any methods along the way, and returning the first `undefined` encountered.
This file contains 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
/** | |
* Recursively navigates into nested collections inside `collection` by | |
* following a sequence of properties `pathArray`, executing any methods | |
* along the way, and returning the first `undefined` encountered. | |
* @param {any} collection - The collection to traverse. | |
* @param {Array<string|number|function()>} pathArray - A sequence of | |
* property names, indexes, or functions to traverse into the `collection`. | |
* @returns {any|undefined} The final value found, | |
* otherwise the first `undefined` encountered. | |
* @example | |
* const largeViewModelObject = { | |
* knockoutObservable: { | |
* GetSomeValue() { return [1, 2, 3] } | |
* } | |
* } | |
* getByPath(largeViewModelObject, ["knockoutObservable", "GetSomeValue", 2]); | |
* // returns 3 | |
*/ | |
const getByPath = (collection, pathArray) => { | |
const [prop, ...leftoverProps] = pathArray; | |
const value = collection[prop]; | |
return leftoverProps.length == 0 | |
? value | |
: getByPath( | |
value instanceof Function ? collection[prop]() : value, | |
leftoverProps | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment