Neat ways to access nested objects with vanilla JavaScript.
const dataObj = {
level1: {
level2: {
level3: {
value: 1234567890,
},
},
},
};
const value3 = dataObj.level1.level2.level3.value; // 1234567890
const value3_2 = dataObj.level1.level2.level3_2.value; // error
const levels = ['level1', 'level2', 'level3', 'value'];
const value = levels.reduce((obj, level) => {
return (obj || {})[level];
}, dataObj);
const getValue = (path, defaultValue) => {
try {
return path();
} catch (error) {
return defaultValue;
}
};
const value = getValue(() => dataObj.level1.level2.level3.value, 'Not found');