4

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.

jane arc
  • 574
  • 3
  • 16
  • related http://stackoverflow.com/questions/14974864/combine-or-merge-json-on-node-js-without-jquery – Unicorn Jun 19 '15 at 02:12

3 Answers3

3

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.

ysth
  • 96,171
  • 6
  • 121
  • 214
3

If you happen to be using jQuery, there's a nice extend function:

$.extend(globaldata, sensitivedata);

Otherwise I suppose you need to roll your own, but it isn't hard. With a recent-enough version of Javascript, it's:

for (let [key, value] in Iterator(sensitivedata))
    globaldata[key] = value;

By the by, in Perl,

@{ $sensitivedata }{ keys %{ $sensitivedata } }

would be more clearly expressed as simply

values %{ $sensitivedata }
Sean
  • 29,130
  • 4
  • 80
  • 105
  • Quite right; I feel being a little pedantic there is clearer. As per usual, of course, there are myriad ways to do it. I am actually using node and not jQuery. – jane arc Jan 27 '14 at 12:08
  • 2
    If there is a library for use in node that provides similar capability to jQuery's .extend and .each, it would be nice to hear about it – ysth Jan 27 '14 at 23:40
  • You can use Underscore's extend method or built in util._extend. see this answer http://stackoverflow.com/questions/14974864/combine-or-merge-json-on-node-js-without-jquery – Unicorn Jun 19 '15 at 02:11
0

I couldn't get Iterator working in node.js, but I did find a workaround to Sean's answer.

for (var key in add_headers)
  http_headers[key] = add_headers[key];
Jon
  • 7,848
  • 1
  • 40
  • 41