Skip to content

Instantly share code, notes, and snippets.

@StefanHamminga
Last active April 9, 2016 11:05
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 StefanHamminga/aea6582e2f90b758a66c6a7ad42cae3f to your computer and use it in GitHub Desktop.
Save StefanHamminga/aea6582e2f90b758a66c6a7ad42cae3f to your computer and use it in GitHub Desktop.
One liner flat object to key="value" conversion in ES6/ES2015 JavaScript
// Any flat object with enumerable properties
var properties = {
id: "myid",
value: "myvalue"
};
// This prepends a space to each key. If you only need separators between add them in the join.
// ↓ ↓
var string = Object.keys(properties).map(function(n){return ` ${n}="${properties[n]}"`;}).join("");
// ' id="myid" value="myvalue"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment