[英]json.stringify object and rename variables in json
I want to serialize an object o
, which has a method called, let's say, a
.我想序列化一个 object o
,它有一个名为a
的方法。 The object also holds a variable, which name is _a
. object 还包含一个变量,其名称为_a
。
I now want to parse this object to a JSON string.我现在想将此 object 解析为 JSON 字符串。 But the JSON looks something like this:但是 JSON 看起来像这样:
{
"_a": "",
...
}
Is there a way, to comfortably remove/ replace the _
character(s) (or any character(s)).有没有办法轻松删除/替换_
字符(或任何字符)。
Object.keys(o).forEach(key => {
Object.defineProperty(o, key.replace("_", ""),
Object.getOwnPropertyDescriptor(o, key));
delete o[key];
});
Would something like this work for you:像这样的东西对你有用吗:
let obj = { "_a": "", "b": "Test" } let result = Object.entries(obj).reduce((acc, [key, value]) => { let newKey = key.replace('_', '') acc[newKey] = value return acc }, {}) console.log(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.