嵌套解构

时间:2017-06-20 18:43:01

标签: javascript ecmascript-6 destructuring

假设我有一个形状如此的物体:

{
  rows: [
    {some: fields,
     go: here}]
}

,并且说,在特定情况下,我知道行的长度是1.我怎么能通过解构来提取{some:fields,go:here}?

我尝试过:{rows: [stuff]}{rows: stuff},但在这两种情况下console.log(stuff)都会打印[{some: fields, go: here}]如何通过解构来实现这一目标?

1 个答案:

答案 0 :(得分:0)

{rows: [stuff]}效果很好:



const obj = {
  rows: [
    {some: 'fields',
     go: 'here'}]
};

const { rows: [stuff] } = obj;
console.log(stuff);