ES5 implements of object optional chaining.
See:
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
Your environment should support Array.prototype.reduce().
MIT © Minwe