5

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!

0

2 Answers 2

8

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)

Sign up to request clarification or add additional context in comments.

3 Comments

@JonasW. wow, didn't mean to steal the spotlight. I was actually about to delete my answer when I noticed you edited and then I got a flood of upvotes o.o;
people love snippets :), and nevermind, zwei dumme ein gedanke :)
I was about to ask about the feasibility of dynamic properties inside object literals, but then the OP is using destructuring.
7
 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;

5 Comments

This is the desired result for sure, I am just curious if you can destructure based off a var. Thanks!
@ajmajmajma you can. const { content } = data is exactly that.
@ajmajmajma actually you can, just looked that up :)
@evolutionxbox no that's not what I mean. The payload returns data.taxonomy.name which is either content or collection. That being dynamic and destructing based on that. Otherwise i could just do const { content, colleciton } = data - see above answers
@ajmajmajma: See Nina's comment, the question is unclear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.