Last active
February 24, 2023 17:40
-
-
Save lucasdavila/4331999 to your computer and use it in GitHub Desktop.
Joining javascript key-value objects as string.
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
Object.prototype.join = function(glue, separator) { | |
var object = this; | |
if (glue == undefined) | |
glue = '='; | |
if (separator == undefined) | |
separator = ','; | |
return Object.keys(object).map(function (key, value) { return [key, value].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
@cccabdalla the
$
was a reference to the global jquery object, the original code is a bit old.I will update it with the suggestion @alitoufighi sent, thanks.