【发布时间】:2020-09-10 04:17:29
【问题描述】:
我有一个对象,即:
{
item1: "value1",
item2: "value2",
item3: {
item4: "value4",
item5: "value5"
}
}
我想将JSON.stringify 与replacer 函数一起使用,该函数将对项目4 和5 采取不同的作用,即项目3 的内部属性。
如何做到这一点?
类似于以下伪代码:
return JSON.stringify(obj, (key, val) => {
if (key is child of Item3) {
return someOtherValue;
} else {
return val;
}
}
所需的输出是 json。即:
{
"item1" : "value1",
"item2" : "value2",
"item3" : {
"item4" : "theSomeOtherValue",
"item5" : "theSomeOtherValue"
}
编辑:
第 4 项和第 5 项事先不知道,它们是动态生成的。
我只知道item3 在运行时的标题
【问题讨论】:
-
想要的输出是什么?
-
所需的输出是 json。即:
{ "item1" : "value1", "item2" : "value2", "item3" : { "item4" : "theSomeOtherValue", "item5" : "theSomeOtherValue" }
标签: javascript json stringify