0

I'm trying to destructure setup shown below. I need to get text: Zelena The Wicked Witch is an enemy to Emma Swan in Once Upon a Time.

OK, it's easy to get properties from objects info and protagonist, but I can't get data from an array of objects (enemies) for particular object, for instance for row number 3. I tried many different expressions, with no luck.

Any help would be highly appreciated.

function nestedArrayAndObject() {
  // refactor this to a single line of destructuring...
  const info = {
    title: 'Once Upon a Time',
    protagonist: {
      name: 'Emma Swan',
      enemies: [
        {name: 'Regina Mills', title: 'Evil Queen'},
        {name: 'Cora Mills', title: 'Queen of Hearts'},
        {name: 'Peter Pan', title: `The boy who wouldn't grow up`},
        {name: 'Zelena', title: 'The Wicked Witch'},
      ],
    },
  }
  
 const {
      protagonist: {
          enemies[3]: {name: enemyName}          
      }, 
      protagonist: {
          enemies: {title: enemyTitle}
      },
      protagonist: {name: protagonistName},
      title: title 
 } = info;
 
return `${enemyName} (${enemyTitle}) is an enemy to ${protagonistName} in "${title}"`
}

nestedArrayAndObject();

2 Answers 2

3

It's not that hard. Take a look at this.

const info = {title: 'Once Upon a Time', protagonist: {name: 'Emma Swan', enemies: [ {name: 'Regina Mills', title: 'Evil Queen'}, {name: 'Cora Mills', title: 'Queen of Hearts'}, {name: 'Peter Pan', title: 'The boy who wouldn\'t grow up'}, {name: 'Zelena', title: 'The Wicked Witch'} ]}};

const {protagonist: {enemies: [,, {name, title}]}} = info;

console.log(name, title)

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

2 Comments

This is not accurate. Here name will be Peter Pan not Zalena
If we are being that precise...First, I interpret row number 3 as index 2 (I know it's different in the code). Secondly, The question says for instance.
1

You can achieve as:

const {
    title,
    protagonist: {
        name: protagonistName,
        enemies: [, , , { name: enemyName, title: enemyTitle }],
    },
} = info;

function nestedArrayAndObject() {
    // refactor this to a single line of destructuring...
    const info = {
        title: 'Once Upon a Time',
        protagonist: {
            name: 'Emma Swan',
            enemies: [
                { name: 'Regina Mills', title: 'Evil Queen' },
                { name: 'Cora Mills', title: 'Queen of Hearts' },
                { name: 'Peter Pan', title: `The boy who wouldn't grow up` },
                { name: 'Zelena', title: 'The Wicked Witch' },
            ],
        },
    };

    const {
        title,
        protagonist: {
            name: protagonistName,
            enemies: [, , , { name: enemyName, title: enemyTitle }],
        },
    } = info;

    return `${enemyName} (${enemyTitle}) is an enemy to ${protagonistName} in "${title}"`;
}

nestedArrayAndObject();

4 Comments

Beside one comma, I don't really see the difference to my answer tbh.
There is difference OP want to use enemyName in string template literal. second here one comma makes a big diffference Your name will come Peter Pan not Zalena
I see, if a variable name counts as difference.
@Behemoth Actually you answer is not accurate though it is a typo. one comma makes a big diffference Your name will come Peter Pan not Zalena

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.