Combining two dictionaries: perl "hashrefslice" syntax in javascript?

I have separate configuration files that are separate, because they contain passwords and other sensitive data, and I do not mind seeing the world. So let's say I have:

sensitivedata = { password : 'foobaz', hostname : 'quux' };
globalconfig  = { timeout : 86400, port : 5190 };

and I want to globalconfighave fields passwordand hostname. I can just do:

globalconfig.hostname = sensitivedata.hostname;
globalconfig.password = sensitivedata.password;

but it is tiring when there are many fields. As 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 can be done with help as well map, but that is exactly what we have hashrefslice syntax for perl. Is there a way with one application to match a bunch of fields from one dictionary to another?

This happens to me when I write this, that I could also do this in perl:

subname( { %$globalconfig, %$sensitivedata } );

... , , "" / javascript. ( , , , sensitivedata globalconfig).

; " ". node , , , .

+4
3

jQuery, extend:

$.extend(globaldata, sensitivedata);

, , , . Javascript :

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

Perl,

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

values %{ $sensitivedata }
+3

I couldn't get Iterator to work in node.js, but I found a workaround for Sean's answer.

for (var key in add_headers)
  http_headers[key] = add_headers[key];
0
source

Source: https://habr.com/ru/post/1523824/


All Articles