combining two dictionaries: perl "hashrefslice" syntax in javascript?
dictionary, hashmap, node.js, perl
Solution
I don't believe javascript provides a native way to do this; if you were not talking about server side, https://github.com/stephenlb/jquery-hashslice/blob/master/jquery.hashslice.js might meet your needs.
Problem
I have separate config files that are separate because one contains passwords and other sensitive data, and one that I don't mind the world seeing. So let's say I have: ``` sensitivedata = { password : 'foobaz', hostname : 'quux' }; globalconfig = { timeout : 86400, port : 5190 }; ``` and I want `globalconfig` to have the fields `password` and `hostname`. I can just do: ``` globalconfig.hostname = sensitivedata.hostname; globalconfig.password = sensitivedata.password; ``` but this is tedious when there are lots of fields. Being a perl programmer, I want to do something like this: ``` @{ $globalconfig }{ keys %{ $sensitivedata } } = @{ $sensitivedata }{ keys %{ $sensitivedata } }; # or ... @{ $globalconfig }{ qw{ password hostname } } = @{ $sensitivedata }{ qw{ password hostname } }; ``` this could be done with `map` just as well, but this is precisely what we have the hashrefslice syntax for in perl. Is there a one-statement way to map a bunch of fields from one dictionary into another? It occurs to me as I write this that I could also do this in perl: ``` subname( { %$globalconfig, %$sensitivedata } ); ``` …which joins the two hashes into a new anonymous hash for purposes of passing their arguments, but I am not sure how to "dereference" a dictionary/hash in javascript. This solution also combines both dictionaries without specifically referencing elements (if I, say, only want some but not all of `sensitivedata` in `globalconfig`). Please be gentle; I don't mean to incite a "my language vs your language" thing. I like node a lot, I'm just trying to borrow idioms I am familiar with and write my code efficiently.