i am trying to implement a function that uses array elements to access specific object properties for a situation like this:
let foo = {
bar:{
baz:"oldVal",
}
}
let coodinates = ["bar", "baz"]
function accessAndModify(obj, coord, newVal){
//should do this: obj[coord[0]][coord[1]]...[coord[coord.length - 1]] = newVal;
}
i have tried the following:
function accessAndModify(obj, coord, ewVal) {
try {
[obj, ...coord].reduce((x, y) => x[y]) = newVal;
} catch (e) {
return false;
}
}
but it produced this error: ReferenceError: Invalid left-hand side in assignment.