2

I wanted to similar thing like

let { * } = {test1: 'test', test2: 'test2'};

The above code should declare two new variables test1 and test2.

Edit1: My concern here is the right side object may have 10 keys. I don't want to do

let { key1, key2 , key3 , ..., key10 } =

Instead, is there any way of getting all the 10 keys?

mani deepak
  • 401
  • 1
  • 4
  • 15
  • 1
    You can do something like this - `let { test1, test2 } = {test1: 'test', test2: 'test2'};` – Sai Krishna Jun 24 '20 at 05:59
  • 3
    You're asking if you can define variables without specifying their names at the point of definition? I sure hope not. That could make code very confusing. – Brian McCutchon Jun 24 '20 at 06:03
  • @BrianMcCutchon Thank you Brian. I just wanted to see if I can do it. – mani deepak Jun 24 '20 at 06:08
  • 1
    Related: [“Variable” variables in Javascript?](https://stackoverflow.com/q/5187530) but I guess you're trying to *undo* the preferred way. No, there is no way to destructure everything because that would lead to a lot of errors. Imagine you had `let foo = 1; let {*} = obj` and `obj = {foo: 2}` - now you'd get an error with the destructuring for redeclaring `foo`. But only in some situations. Inconsistent bugs are worse than most others. Another problem `let {*} = obj; console.log(foo)`. Now this is either a runtime error or not based on what `obj` contains. Again, inconsistent. – VLAZ Jun 24 '20 at 06:27

2 Answers2

3

Destructuring objects in ES6: Destructuring on objects lets you bind variables to different properties of an object. There is a helpful syntactical shortcut for when the property and variable names are the same.

let { test1, test2 } = {test1: 'test', test2: 'test2'};
console.log(test1, test2);

Below a simple two properties object example for destructuring the properties dynamically.

const obj = { prop1: "value1", prop2: "value2" };

const  propName1 = "prop1";
const  propName2 = "prop2";

// destructure dynamic property
const { [propName1]: val1, [propName2]: val2 } = obj;
console.log('destructure dynamic property values : ', val1, val2);
solanki...
  • 4,982
  • 2
  • 27
  • 29
  • The original answer before edit was more clear: `let { test1, test2 } = {test1: 'test', test2: 'test2'}; console.log(test1, test2);` – mangesh Jun 24 '20 at 06:21
  • 1
    Destructuring also allows you to bind different names: `let {foo: bar} = {foo: "hello"}; console.log(bar);` – VLAZ Jun 24 '20 at 06:29
  • @VLAZ I'm sorry I had my own answer, and wanted to edit my answer, but edited this answer by accident. I'm still half asleep obviously .... thanks for fixing it! :) – eol Jun 24 '20 at 06:34
  • @eol that's OK, we all make mistakes. Just be more careful in the future. Maybe have your coffee first before answering :) – VLAZ Jun 24 '20 at 06:35
3

There is no currently syntax for something like that: you have to be explicit. Even for importsyntax doesn't exists something like that, that would import all the "keys" (exported value) in the scope without being explicit (declaring which "keys" import, or assign the whole module to an object).

If you need most of the keys of an object, probably the best way it would be just use the object directly (o.test1) without destructuring.

Of course in case you have access to the current scope object, such as globalThis, you can always doing something like:

Object.entries(o).forEach(([key, value]) => globalThis[key] = value)

But first, it will work only if you're assign the object value to the global object (so no local scope); and second I don't see how it's different writing this code and then using test1 instead of avoid such code and write o.test1. (And, of course it's something really horrible to write that you shouldn't never do it, I added just for completition).

ZER0
  • 24,846
  • 5
  • 51
  • 54