解构的简单使用

  1. 将name,age提取出来,剩余的放到新对象中
student={
    "name": "Kang",
    "age":19,
    "location":{
        "address": "阿伯",
        "city": "相关",
        "state": "a"
    },
    "website": "www.kang"
}

const {name, age,...obj}=student
  1. 获取数组下的对象
country = {
    "name": "国家", "content": "内容",
    "comments": [{
        "names": "中国", "province": {
            "id": 1, "name": "北京"
        }
    }, {
        "names": "美国", "province": {
            "id": 1, "name": "纽约"
        }
    }]
}

// 获取第一个对象,在数组中加入,代表第二个对象
const {
    comments:[{
        names, province: {
            name
        }
    }]
} = country

# 获取第一个数组元素
const {names,province:{id}}= country.comments[0]
posted @ 2023-05-18 17:39  Kang_kin  阅读(17)  评论(0)    收藏  举报