In NodeJS, I need to declare a long list of variables based on the contents of a matrix/object. The usual way to do this involves adding them to the global scope.
OLD WAY:
let map = { 'a': 1, 'b': 2, 'c': 3 };
for ( let v in map ){
global[ v ] = map[ v ];
}
For my purposes, they cannot be global scope. I'm wondering if there's any new way/trick to accomplish this goal in ES6.
For example, destructuring:
for ... // add the function to a local array (A)
let variableNames = [ 'a', 'b', 'c' ];
let [ ...variableNames ] = A;
If nothing like this can be done, then I'll have to think of a different approach.
EDIT
I forgot to clarify that the variables names are dynamic, so current way of de-structuring does not work.
The only left-hand assignment I know that allows for a variable's value to be used as the new variable's name is with object properties:
let o = {}; o[ variable ] = 'something';
I looked around some more, and it seems that I'm asking to dynamically add to the function context/local scope, which is not currently permitted.