-
-
Save cccabdalla/6a8db0d635594d746019a050190497c1 to your computer and use it in GitHub Desktop.
Joining javascript key-value objects as string.
This file contains hidden or 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
Object.prototype.join = function(glue, separator) { | |
var object = this; | |
if (glue == undefined) | |
glue = '='; | |
if (separator == undefined) | |
separator = ','; | |
return $.map(Object.getOwnPropertyNames(object), function(k) { return [k, object[k]].join(glue) }).join(separator); | |
} | |
var options = { id : 1, name : 'lucas', country : 'brasil'}; | |
options.join(); | |
> "id=1,name=lucas,country=brasil" | |
options.join('=>', ' '); | |
> "id=>1 name=>lucas country=>brasil" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment