Skip to content

Instantly share code, notes, and snippets.

@Artistan
Forked from lucasdavila/object_join.js
Created May 4, 2016 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Artistan/6aa7d9cf7989c8b0454ce1e43a3a5bad to your computer and use it in GitHub Desktop.
Save Artistan/6aa7d9cf7989c8b0454ce1e43a3a5bad 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