Skip to content

Instantly share code, notes, and snippets.

@yy-dev7
Created March 21, 2017 08:23
Show Gist options
  • Save yy-dev7/55b8752430a0ae4885e9b8a158555f5f to your computer and use it in GitHub Desktop.
Save yy-dev7/55b8752430a0ae4885e9b8a158555f5f to your computer and use it in GitHub Desktop.
function optionalChaining(obj, chain) {
return chain
.split('.')
.reduce(function(acc, val) {
return acc ? acc[val] : undefined;
}, obj);
}
var user = { address: { street: 'No.969 West WenYi Road', }, a: { b: { c: 2 } }, }
var ret = optionalChaining(user, 'address');
console.log(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment