-
-
Save elclanrs/7fcb7f3aae2c57d0af94 to your computer and use it in GitHub Desktop.
Dynamic object key interpolation
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
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