简体   繁体   English

使用嵌套数组解构 object

[英]Destructuring an object with nested array

Basically I want to destruct this object to get this result but in the console I see u is not defined基本上我想破坏这个 object 以获得这个结果但是在控制台中我看到u is not defined

The object: 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 My Best Release Is Oath In Felghana 它在美国和日本发行

what I get我得到了什么

Uncaught ReferenceError: u is not defined未捕获的 ReferenceError:您未定义

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任务中提到你需要使用键和值,所以我试着把它放在 object 和我的解构之间

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

but still doesn't work.但仍然不起作用。

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

console.log(u, j)

or if You also need o :或者如果您还需要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.解构内部的o = [ u, j ]作为赋值。

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 . inb4“我可以只写一次密钥吗?”:不可以。据我所知,您必须复制密钥“Oath In Felghana”才能创建别名o并将其解构为uj

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.首先,我需要获得“Feleghana 的誓言”作为元素。 I used Object.entries() method to create an array from releases, then I destructured that array to get the real array.我使用 Object.entries() 方法从发布中创建一个数组,然后我解构该数组以获得真正的数组。

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)]这将返回 ['Oath In Felghana', Array(2)],如果我没有在此处进行解构,它将返回 [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直到这一部分,我只得到在你的 console.log 中可用的 'Oath In Felghana' 字符串

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.在这里,我将版本 object 中的数组重命名,以便您可以在字符串中使用它。

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.最重要的是,在解构对象时,您需要使用与目标元素完全相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM