I want to serialize an object o, which has a method called, let's say, a. The object also holds a variable, which name is _a.
I now want to parse this object to a JSON string. But the JSON looks something like this:
{
"_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)