1

Basically I want to destruct this object to get this result but in the console I see u is not defined

The object:

const game =
    releases: {
      "Oath In Felghana": ["USA", "Japan"],
     };`

My code:

const {
    releases: {
      "Oath In Felghana": o = [u, j],
 } = game;

console.log(`My Best Release Is ${o} It Released in ${u} & ${j}`);

What I want to see

My Best Release Is Oath In Felghana It Released in USA & Japan

what I get

Uncaught ReferenceError: u is not defined

So the problem is that it shows me that you is undefined even though I used array destructuring to destruct it

It's mentioned in the task that you need to use key and values, so I tried to put this between the object and my destructuring

game.releases["Oath In Felghana"] = Object.keys(game.releases["Oath In Felghana"]);

but still doesn't work.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
zakramy
  • 19
  • 2
  • First of all, your "object" is not an object but a string. Not sure if that's just a formatting issue here, though ... – derpirscher Nov 20 '22 at 08:03
  • @derpirscher just formatting issue. If it was a string, there would have been a different error. `Uncaught ReferenceError: u is not defined` means it was code which was processed. – VLAZ Nov 20 '22 at 08:13
  • @VLAZ probably. But we have all seen many many questions where the code and the alleged error message don't fit together. And even how it's written now, it's still invalid ... – derpirscher Nov 20 '22 at 08:15
  • @derpirscher then OP managed to accidentally use string instead of code and then reported an error that would only ever happen if it was code instead of string. I find the chances of this significantly lower than simply a problem with the formatting. I also take into account that it was created through the Ask Wizard where the editor is *very* fond of adding extra backticks where they aren't needed. But maybe you're right and I shouldn't have edited - if you think initial version was indeed 100% accurate representation, then feel free to roll back. – VLAZ Nov 20 '22 at 08:20
  • @VLAZ No, it's not about your edit. I wanted call OP's attention to be more carefull when formatting code. Because it's not just about some unnecessary backticks but the missing braces also. While it might be quite easy to guess the actual code here, there are many questions out there, where those errors are more subtle, or even lead to code which seems valid but has a totally different semantcis ... Thus, I tend not to edit such code, but ask OP to fix it ... – derpirscher Nov 20 '22 at 08:27

2 Answers2

0
const { releases: { "Oath In Felghana" : [u, j] } = game

console.log(u, j)

or if You also need o:

const { releases: { "Oath In Felghana" : o, "Oath In Felghana" : [u, j] } = game

console.log(o, u, j)

The o = [ u, j ] inside destructuring works as assignment.

inb4 "can I write key only once?": No. AFAIK You have to duplicate the key "Oath In Felghana" to both create an alias o and destructurize it into u and j.

0

I am studying "destructuring", so I decided to take this question as a challenge. My solution has two steps. First to get the entry name, then the other parts. The first answer is better, I just wanted to put my answer here as a proof that I completed this challenge.

First of all, I needed to get the "Oath in Feleghana" as an element. I used Object.entries() method to create an array from releases, then I destructured that array to get the real array.

const [i] = Object.entries(game.releases);

this will return ['Oath In Felghana', Array(2)], If I didn't destructure here it would return [Array(2)]

The second part was destructuring the array I got, to get the release name.

const [o] = i

Till this part I only get the 'Oath In Felghana' string which is usable in your console.log

The second part is to get other elements.

 const {['Oath In Feleghana']: q} = game.releases;

Here I am renaming the array inside the releases object, so you can use it in your string.

Then, I destructured the array.

const [u,j] = q;

This way I created all the variables you need to create your string.

The most important thing is that while destructuring objects, you need you need to use the exact same name with the target element.