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();
Adnan
  • 61
  • 1
  • 9

2 Answers2

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)
Behemoth
  • 5,389
  • 4
  • 16
  • 40
  • This is not accurate. Here `name` will be `Peter Pan` not `Zalena` – DecPK Aug 05 '22 at 10:51
  • 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*. – Behemoth Aug 05 '22 at 10:54
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();
DecPK
  • 24,537
  • 6
  • 26
  • 42
  • Beside one comma, I don't really see the difference to my answer tbh. – Behemoth Aug 05 '22 at 10:44
  • 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` – DecPK Aug 05 '22 at 10:46
  • I see, if a variable name counts as difference. – Behemoth Aug 05 '22 at 10:47
  • @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 – DecPK Aug 05 '22 at 10:48
  • And it gets accepted I could cry. A perfect example of spoon feeding answer stealing. – Behemoth Aug 05 '22 at 11:50