Last active
August 29, 2015 14:20
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!