Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript object destructuring and aliasing

Tags:

Is there a way to destructure an object in JavaScript and alias the local destructured object?

Something like:

const env = {ENV_VAR_X, ENV_VAR_Y, ENV_VAR_Z} = process.env;

...and have env become a local constant containing those selected environment variables. (I'm aware that my example doesn't work with babel)

{
  ENV_VAR_X: "s867c7dsj4lal7", 
  ENV_VAR_Y: "hd73m20s-a=snf77f", 
  ENV_VAR_Z: "production"
}

Is there a way to achieve this aliasing?

On a side note, I'm using babel as my transpiler, then running the script with node so that I can leverage more ECMAScript 6 functionality.