Write get(object, path, defaultValue) to read a nested value by a path like a.b[0].c.
path is a string with dots and bracket indexes, or an array of keys. Return defaultValue when a step along the way is null or undefined, or when the value found is undefined. A stored null, 0, "", or false is returned as it is.
function get(object, path, defaultValue) {
// your code
}
// const user = { posts: [{ title: "Hi", tags: ["js"] }] };
// get(user, "posts[0].tags[0]"); // "js"
// get(user, ["posts", 0, "title"]); // "Hi"
// get(user, "posts[1].title", "n/a"); // "n/a"
Solve it in the editor, with tests
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…