简体   繁体   English

数组中的嵌套对象-破坏es6的对象

[英]Nested object in array - object destructing es6

So I know you can do object destructing like: const { item } = data; 因此,我知道您可以进行对象破坏,例如: const { item } = data;

Also array destructing like: const [ item ] = data; 还可以像这样破坏数组: const [ item ] = data;

You can also do this in function params like: const x = ({ item }) => item; 您也可以在函数参数中执行以下操作: const x = ({ item }) => item;

And I've seen lots of questions and answers on it. 而且我已经看到了很多关于它的问题和答案。 However I haven't seen an example and a good explanation of nested objects in an array. 但是,我还没有看到数组中嵌套对象的示例和很好的解释。


const test = [{ count: 1 }];

const [{ count }] = test;

I'd normally do: 我通常会这样做:

const x = test[0];

const { count } = x;

It was only today while testing in codepen that I figured out you could destructor them both within the same assignment. 直到今天,在Codepen中进行测试时,我才知道您可以在同一任务中销毁它们。

Could anyone explain what's going on when I'm doing [{ count }] ? 有人能解释我在做[{ count }]什么吗? Because I'm doing array destructing with the const [] = test but I'm not destructing anything so that obviously fails. 因为我正在用const [] = test进行数组销毁,但是我没有销毁任何东西,所以显然失败了。 If I then { count } within that I get the value out I want. 如果我在其中{ count } ,我就会得到我想要的价值。

I can't break it down enough to understand how it's working. 我无法将其分解得足以了解其工作原理。 I'd assume [] = test is the same as test[0] then i do { count } = test[0] . 我假设[] = testtest[0]相同,然后我做{ count } = test[0] But I'd just like to understand how it's working more. 但我只想了解它的工作原理。

I did have a look through some of the MDN docs and stuff but I can't find a good explanation on the above scenario I mentioned. 我确实浏览了一些MDN文档和资料,但是找不到上面提到的方案的很好的解释。

Thanks! 谢谢!

Nested destructuring can be confusing sometimes. 嵌套的销毁有时会造成混乱。 You can always check on the Babel compiler to get the ES5 equivalent and understand how it works 您可以随时检查Babel编译器以获取等效的ES5并了解其工作原理

So, this code: 因此,此代码:

 const test = [{ count: 0 }, { whatever: 1 }, { total: 2 }]; const [{ count }, , { total }] = test console.log(count, total) 

Gets trasnpiled to: 被转移到:

var count = test[0].count;
var total = test[2].total;

As you can see, index = 1 item is ignored (MDN) and we are only destructuring the 0 th and 2 nd index properties 如您所见, index = 1项被忽略(MDN) ,我们仅在构造第0和第2索引属性

Since, we are on the topic of destructuring array objects, this can be used in much more advanced ways. 因为我们的主题是销毁数组对象,所以可以用更高级的方式使用它。 You can destructure an item at any index like this: 您可以使用以下任何索引来破坏项目:

 const test = [{ count: 0 }, { count: 1 }, { count: 2 }]; const { 2: { count } } = test; console.log(count) 

This gets the count at index 2 . 这将在索引2处获得计数。 This code is equivalent to: 此代码等效于:

var count = test[2].count;

Note that, we are using {} here instead of [] . 请注意,我们在这里使用{}而不是[] This instructs the compiler to get the count at the key : 2 . 这指示编译器从处获取count2 You can also get the length of the array using this type of destructuring: 您还可以使用这种类型的解构来获取数组的length

const { length } = test; // same as test.length 

You can make it even more dynamic with a computed object property name : 您可以使用计算对象属性名称使其更加动态:

 const test = [{ count: 0 }, { count: 1 }, { count: 2 }]; const index = 2; const { [index]: { count } } = test; console.log(count) 

It might make things easier if you think of Object & Array destructuring as the reverse operation of object & array creation: 如果将对象和数组的结构分解视为对象和数组创建的相反操作,则可能会使事情变得更容易:

 ({ key: value  } = // reverses building object with "key"
  { key: "test" }); // builds up object with "key"

 ([{ nested: value2 }] = // reverses building an array containing an object
  [{ nested: "test" }]); // builds up an array containing an object

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

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