Created
January 19, 2017 02:14
-
-
Save hapticdata/5ec635b41fdaa31001408cf95307150f to your computer and use it in GitHub Desktop.
safely walk an objects hierarchy returning false if any step doesnt exist and assert the final value is equal to other paramter
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
/** | |
* a utility method that will walk the object for the provided properties | |
* safely asserting if the final property is equal to `other` | |
* @param {Object} obj | |
* @param {Array} steps | |
* @param {Object} other | |
* @returns {boolean} true only if properties exist for object to | |
* be walked and final prop is equal to `other` | |
*/ | |
function walkEquals(obj, steps, other){ | |
if(!obj){ | |
return false; | |
} | |
for(let i=0; i<steps.length; i++){ | |
const next = obj[steps[i]]; | |
if(!next){ | |
return false; | |
} | |
obj = next; | |
} | |
return obj === other; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment