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" |
alitoufighi
commented
Aug 30, 2020
Object.keys(object).map((key) => [key, options[key]].join(glue)).join(separator)
@alitoufighi, I was wondering where $ came from but your fix is very helpful and worked as expected. Thank U.
@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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment