js破坏子对象内部对象

时间:2017-09-21 02:31:49

标签: javascript node.js object ecmascript-6

我指定了一个对象:

const info = { name: 'Peter', location: { province: 1, city: 2 } };
let { name } = info;
console.log(name); // 'Peter'

// then how to get location.province
let { 'location.province': province } = info;
console.log(province); // 'undefined'

如何通过解构???

获取子对象location.province

1 个答案:

答案 0 :(得分:1)

通过“嵌套”解构:

let {name, location: {province}} = info;

对于这些问题,请先查看MDN,因为它通常是has many examples