const ob = {user: {name: 'Jane', extension: '1234'}};
const param = 'name';
const path = `ob.user.${param}`;
console.log(path);
So this prints "ob.user.name". What I want is for it to print "Jane". I can do that if I call eval()
on path
, but I'm told that eval()
is slow, insecure, and possibly carcinogenic.
How can I interpolate a value into path
and get to the value to which path
refers without using eval()
?
(The code in question will be going inside a React component, which I think precludes any solutions the window object.)