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.

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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 }

3 Comments

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.
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
You can use Underscore's extend method or built in util._extend. see this answer stackoverflow.com/questions/14974864/…
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];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.