假设我们有以下对象:
const page = {
name: 'Page 1',
page_category: [
{
postId: 1,
catId: 1,
category: {
id: 1,
name: 'category 1'
}
},
{
postId: 3,
catId: 2,
category: {
id: 2,
name: 'category 2'
}
},
]
}
Run Code Online (Sandbox Code Playgroud)
要以解构方式获取 page_category 数组中的第一个对象,我们将这样做:
const { page_category: [first] } = page
Run Code Online (Sandbox Code Playgroud)
但是如果我们想获取第一个对象的类别字段会怎样呢?