Last active
April 14, 2018 22:53
-
-
Save egoarka/38c6c1609b1361efc6831bf36355b21c to your computer and use it in GitHub Desktop.
notmaybe
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
const data = { user: { address: { street: "my street" } } }; | |
const data2 = { user: {} }; | |
const proxy = obj => new Proxy(obj, { get }); | |
const get = (t, p) => | |
t && t[p] && (typeof t[p] === "object") ? proxy(t[p]) : t[p] ? t[p] : ""; | |
const safe = obj => proxy(obj); | |
var out = safe(data).user.address.street; | |
var out2 = safe(data2).user.address.street; | |
console.log(out); // my street | |
console.log(out2); // undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment