How to destructure into dynamically named variables in ES6?
<p>Let's suppose I have the following object:</p>

<pre><code>const user = { 
  id: 42, 
  displayName: "jdoe",
  fullName: { 
      firstName: "John",
      lastName: "Doe"
  }
};
</code></pre>

<p>And that I want only the <code>id</code> and <code>fullName</code>.</p>

<p>I will do the following :</p>

<pre><code>const { id, fullName } = user
</code></pre>

<p>Easy-peasy, right?</p>

<p>Now let's suppose that I want to do the destructuring based on the value of another variable called <code>fields</code>.</p>

<pre><code>const fields = [ 'id', 'fullName' ]
</code></pre>

<p>Now my question is : <strong>How can I do destructuring based on an array of keys?</strong></p>

<p>I shamelessly tried the following without success:</p>

<p><code>let {[{...fields}]} = user</code> and <code>let {[...fields]} = user</code>. Is there any way that this could be done?</p>

<p>Thank you</p>