Skip to content

Instantly share code, notes, and snippets.

@elclanrs
Created June 4, 2014 02:08
Show Gist options
  • Save elclanrs/7fcb7f3aae2c57d0af94 to your computer and use it in GitHub Desktop.
Save elclanrs/7fcb7f3aae2c57d0af94 to your computer and use it in GitHub Desktop.
Dynamic object key interpolation
var formatObj = function(data, obj) {
return Object.keys(obj).reduce(function(acc, k) {
acc[data[k.match(/#\{(\w+)\}/)[1]]] = obj[k]
return acc
},{})
}
formatObj({a:'foo', b:'baz'}, {'#{a}': 'this is foo'
,'#{b}': 'this is baz'})
//^
// {foo: "this is foo"
// ,baz: "this is baz"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment