Skip to content

Instantly share code, notes, and snippets.

@cccabdalla
Forked from lucasdavila/object_join.js
Created January 21, 2021 20:43
Show Gist options
  • Save cccabdalla/6a8db0d635594d746019a050190497c1 to your computer and use it in GitHub Desktop.
Save cccabdalla/6a8db0d635594d746019a050190497c1 to your computer and use it in GitHub Desktop.
Joining javascript key-value objects as string.
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