Implement a function get(obj, path, defaultValue) that safely accesses deeply-nested properties in JavaScript objects.
The function should:
a.b.c) and array indexes (a.b[0].c).Examples
const obj = { a: { b: { c: 3 } } }; get(obj, 'a.b.c'); // 3 get(obj, 'a.b.d', 'default'); // 'default' get(obj, ['a', 'b', 'c']); // 3 get({ a: { b: [10, 20, { c: 30 }] } }, 'a.b[2].c'); // 30 get(null, 'a.b', 'fallback'); // 'fallback'
JavaScript Function
No test results yet
Click "Run" to execute tests