object-optional-chaining
v1.0.1
Published
ES Optional Chaining
Downloads
78
Maintainers
Readme
ES Object Optional Chaining
ES5 implements of object optional chaining.
See:
Usage
import optionalChaining from 'object-optional-chaining';
const user = {
id: '0001',
name: 'Minwe',
repos: {
total: 5,
list: [
{ id: '1001', name: 'Repo1' },
{ id: '1002', name: 'Repo2' },
{ id: '1003', name: 'Repo3' },
{ id: '1004', name: 'Repo4' },
{ id: '1005', name: 'Repo5' },
],
},
};
optionalChaining(user, 'name'); // Minwe
optionalChaining(user, 'repos.total'); // 5
optionalChaining(user, 'repos.list'); // array
optionalChaining(user, 'repos.list[0]'); // { id: '1001', name: 'Repo1' }
optionalChaining(user, 'repos.list[0].id'); // 1001
optionalChaining(user, 'repos.notExist'); // undefined
Note
Your environment should support Array.prototype.reduce().
License
MIT © Minwe