I've got this JSON data object (example):
let obj = [
{
test0: [
{testA: 'ContentA' },
{testB: 'ContenB'}
]
}
];
I'd like to replace the value of key 'testB' dynamically. I.e. I need to be able to address any item in the object and replace ist.
Here's what I am doing so far:
Therefore I programmatically generate a mixed key/index path to address the target object and it looks like this (in array form):
let path = [0,'\'test0\'',1,'\'testB\''];
In order to put this in an executable form which does the actual replacement, I convert path to JavaScript code and run it with eval. Like this:
let newText = 'ContentB';
eval(`obj[${path.join('][')}]=\'${newText}\'`);
Eval executes this code literal:
obj[0]['test0'][1]['testB'] = 'ContentB'
This works, but if possible, I'd like to know if there's an alternative way which works without using "eval".
jsontag.