我有一个对象,即:
{
item1: "value1",
item2: "value2",
item3: {
item4: "value4",
item5: "value5"
}
}
Run Code Online (Sandbox Code Playgroud)
我想JSON.stringify与一个replacer函数一起使用,该函数对 item3 的内部属性4 和 5 有不同的作用。
那怎么办?
类似于以下伪代码:
return JSON.stringify(obj, (key, val) => {
if (key is child of Item3) {
return someOtherValue;
} else {
return val;
}
}
Run Code Online (Sandbox Code Playgroud)
所需的输出是 json。IE:
{
"item1" : "value1",
"item2" : "value2",
"item3" : {
"item4" : "theSomeOtherValue",
"item5" : "theSomeOtherValue"
}
Run Code Online (Sandbox Code Playgroud)
编辑:
项目 4 和 5 事先未知,它们是动态生成的。
我只知道item3运行时的标题