Skip to content

Instantly share code, notes, and snippets.

@vincentzierigen
Last active August 29, 2015 14:20
Show Gist options
  • Save vincentzierigen/535672203fcd784930fb to your computer and use it in GitHub Desktop.
Save vincentzierigen/535672203fcd784930fb to your computer and use it in GitHub Desktop.
Trims every string of an object, at any level (inside Arrays or even inside other objects)
/*
* Trim Object
* Trims all strings in all levels
* @param {Object} dirty
* @returns {Object} clean
*/
function trimObject(object){
if(typeof object === 'string'){
object = object.trim().replace(/\t/g,'').replace(/\n/g,'');
}else if(object instanceof Array){
object = object.map(function(e){
return trimObject(e);
});
}else if(object instanceof Object){
var keys = Object.keys(object);
for(var k in keys){
var key = keys[k];
var temp = object[key];
object[key] = trimObject(temp);
}
}
return object;
}
@pulgaroja
Copy link

So helpful!!! Thank you so much!!!

@osmans
Copy link

osmans commented Apr 29, 2015

Very useful! Thanks a lot!

@esolorzanob
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment