Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript (ES6), destructure based off variable

I am wondering to see if there is a way to destructure objects in javascript with using a variable. Where as I was doing something like this in my function -

mutateTaxon(data) {
    const { content } = data;
    const { plp } = content || {};
    ...

This was working fine, however I need to expand this function based off another factor that can change if I need to use data.content (which it is using now) or data.collection. So I have another node on the data - which changes call to call. I am trying something like this -

mutateTaxon(data) {
    const match = lowerCase(data.taxonomy.name);
    const { match } = data;
    const { plp } = match || {};

Where that match variable would evaluate to either content or collection (as expected). This does not seem to work however, maybe it is not possible? I was thinking maybe the match var needed to be evaluated so I tried something like -

const { [[match]] } = data;

which also is not working. Maybe this is not possible, or I am approaching this wrong. I am wondering, is something like this possible? Thanks!

like image 371
ajmajmajma Avatar asked Apr 05 '18 15:04

ajmajmajma


People also ask

Can we Destructure function in JavaScript?

The destructuring is also possible for JavaScript Arrays. By default, the object key name becomes the variable that holds the respective value. So no extra code is required to create another variable for value assignment.

How do you Destructure a list of objects in JavaScript?

Destructuring in Objects When destructuring objects, we use curly braces with the exact name of what we have in the object. Unlike in arrays where we can use any variable name to unpack the element, objects allow just the use of the name of the stored data.

Can we Destructure array in JavaScript?

Destructuring is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. That is, we can extract data from arrays and objects and assign them to variables.


2 Answers

The destructuring syntax would, as Jonas W. said, be a bit more cumbersome than the bracket notation, but nonetheless, this is how you would do it:

mutateTaxon(data) {
  const key = lowerCase(data.taxonomy.name);
  const { [key]: { plp } = {} } = data;

Demo

const foo = { bar: { plp: 'success' } }
const key = 'bar'
const { [key]: { plp } = {} } = foo

console.log(plp)

And to confirm that default parameter = {} works as expected:

const foo = { }
const key = 'bar'
const { [key]: { plp } = {} } = foo

console.log(plp)
like image 153
Patrick Roberts Avatar answered Oct 24 '22 17:10

Patrick Roberts


 const key = lowerCase(data.taxonomy.name);
 const match = data[key];

I dont think that object destructuring is useful here. But if you need that:

  const key = lowerCase(data.taxonomy.name);
  const {[key]: match} = data;
like image 37
Jonas Wilms Avatar answered Oct 24 '22 17:10

Jonas Wilms