lodash.get shows up constantly for reading API responses defensively. The question tests light parsing plus careful traversal.
Two phases:
[0] into dot access, split on ., and drop empty segments.null/undefined, stop and return the default — that's what makes it "safe".?. needs a static path; get takes a dynamic runtime path.set(obj, path, value)?" → walk the same keys, creating missing objects/arrays as you go.a["b.c"]?" → a real tokenizer, worth flagging as the harder version.Implement get, then the matching set that creates intermediate containers.
function get(obj, path, defaultValue) { // path: "a.b[0].c" or ["a", "b", 0, "c"] } const data = { a: { b: [{ c: 42 }] } }; console.log(get(data, 'a.b[0].c')); // 42 console.log(get(data, 'a.b[1].c', 'n/a')); // "n/a"
Test Code
Enter JavaScript that runs after your solution. It should return a value or a Promise.