Last active
April 9, 2016 11:05
-
-
Save StefanHamminga/aea6582e2f90b758a66c6a7ad42cae3f to your computer and use it in GitHub Desktop.
One liner flat object to key="value" conversion in ES6/ES2015 JavaScript
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
// 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