Created
August 25, 2018 13:10
-
-
Save daniel12fsp/665b7fcae2a872945349db921a1f5eb0 to your computer and use it in GitHub Desktop.
Check if props is in nested 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
| //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