小编Nod*_*ros的帖子

在 JavaScript 中解构数组的第一个对象

假设我们有以下对象:

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)

但是如果我们想获取第一个对象的类别字段会怎样呢?

javascript arrays destructuring ecmascript-6

0
推荐指数
1
解决办法
398
查看次数

标签 统计

arrays ×1

destructuring ×1

ecmascript-6 ×1

javascript ×1