How do you trim white spaces in both the keys and values in a JavaScript Object recursively?
I came across one issue in which I was trying to \"clean\" a user suppli
The best solution I used is this. Check the documentation on replacer function.
var obj = {"data": {"address": {"city": "\n \r New York", "country": " USA \n\n\r"}}};
var trimmed = JSON.stringify(obj, (key, value) => {
if (typeof value === 'string') {
return value.trim();
}
return value;
});
console.log(JSON.parse(trimmed));