There is an object set up like this:
{
item1: "value1",
item2: "value2",
item3: {
item4: "value4",
item5: "value5"
}
}
The goal is to utilize JSON.stringify
along with a replacer
function that behaves differently for items 4 & 5, which are the inner properties of item3.
How can this be achieved?
A hypothetical approach could look something like the code below:
return JSON.stringify(obj, (key, val) => {
if (key is child of Item3) {
return someOtherValue;
} else {
return val;
}
}
The expected output in json format would be:
{
"item1" : "value1",
"item2" : "value2",
"item3" : {
"item4" : "theSomeOtherValue",
"item5" : "theSomeOtherValue"
}
Edit:
Items 4 & 5 are dynamically generated and not known in advance. Only the title for item3
is known at runtime.