Created
August 6, 2014 19:29
-
-
Save UziTech/3b926e732e3a6391b2ab to your computer and use it in GitHub Desktop.
Trim all strings in an object
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
/** | |
* DWTFYW License | |
* | |
* Author: Tony Brix, http://tonybrix.info | |
* | |
* This function trims all strings in an object or array | |
* | |
* This is useful when sending ajax requests | |
* | |
* var vals = { | |
* this: $("#this").val(), | |
* that: $("#that").val(), | |
* the: { | |
* other: $("#other").val(), | |
* thing: $("#thing").val() | |
* } | |
* } | |
* | |
* trimAll(vals); | |
* | |
* $.post("someurl.php", vals, function(data){ | |
* alert("do something"); | |
* }, "json"); | |
* | |
*/ | |
function trimAll(obj) { | |
for (var prop in obj) { | |
if (typeof obj[prop] === "string") { | |
obj[prop] = obj[prop].trim(); | |
} else { | |
trimAll(obj[prop]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment