Skip to content

Instantly share code, notes, and snippets.

@cocodrino
Created January 14, 2015 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocodrino/a25dde47e964d82fc376 to your computer and use it in GitHub Desktop.
Save cocodrino/a25dde47e964d82fc376 to your computer and use it in GitHub Desktop.
remove trailing whitespaces from object in javascript (works recursively)
var fixKeys = function(obj) {
return Object.keys(obj).reduce(function(mem,key) {
if(Array.isArray(obj[key])){
mem[key.trim()] = obj[key].map(function(v){return v.trim()})
}else{
if(typeof obj[key] === 'object'){
mem[key.trim()] = fixKeys(obj[key])
}else{
mem[key.trim()] = (obj[key]).trim()
}
}
return mem
},{})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment