Skip to content

Instantly share code, notes, and snippets.

@daniel12fsp
Created August 25, 2018 13:10
Show Gist options
  • Save daniel12fsp/665b7fcae2a872945349db921a1f5eb0 to your computer and use it in GitHub Desktop.
Save daniel12fsp/665b7fcae2a872945349db921a1f5eb0 to your computer and use it in GitHub Desktop.
Check if props is in nested object
//It's not the best implementation because will iterate over all props.
// If one props is undefined, so it's uncessary evaluate others props
function hasProps(obj, props){
return props.split(".").reduce((result, prop)=> result && result[prop], obj)
}
//Example
/*
const obj = {a:{b:{c:{d:1}}}}
obj.a.b.c.d === hasProps(obj, "a.b.c.d")
obj.a.b.c.e.t.h.j // will throw a error because undefined e props don't have props
hasProps(obj, "a.b.c.e.t.h.j") === undefined // without error =)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment