Skip to content

Instantly share code, notes, and snippets.

@hapticdata
Created January 19, 2017 02:14
Show Gist options
  • Save hapticdata/5ec635b41fdaa31001408cf95307150f to your computer and use it in GitHub Desktop.
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
/**
* 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