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
@RobG Thank you for the solution. Adding one more condition will not create more nested objects
function trimObj(obj) {
if (obj === null && !Array.isArray(obj) && typeof obj != 'object') return obj;
return Object.keys(obj).reduce(function(acc, key) {
acc[key.trim()] = typeof obj[key] === 'string' ?
obj[key].trim() : typeof obj[key] === 'object' ? trimObj(obj[key]) : obj[key];
return acc;
}, Array.isArray(obj)? []:{});
}