I have JSON with references to other parts in that JSON file.
// the function I would like to be reviewed
function referenceToObject(json, reference){
return (new Function(
'json', 'return json' + reference
))(json);
}
// json
var example = {
"target": "['first']['second']",
"first": {
second: 'hi'
}
};
// execution
console.log(referenceToObject(example, example.target));
That function basically executes like this:
return json['one']['two'];
Is there a better way to extract the JSON
reference? I'm able to change every bit of this code, as well as the JSON
because we are reworking the front- and the backend. So, if you have ideas to improve the JSON
instead of the Javascript
that would also be great.
target
is formatted in that way? \$\endgroup\$reduce
there to get the location returned (just learned that function) or should I use a different approach? \$\endgroup\$