Destructuring and rename property

人走茶凉 提交于 2020-01-29 14:10:38

问题


const a = {
 b: {
  c: 'Hi!'
 }
};

const { b: { c } } = a;

Is it possible rename b in this case? I want get c and also rename b.


回答1:


You could destructure with a renaming and take the same property for destructuring.

const a = { b: { c: 'Hi!' } };
const { b: formerB, b: { c } } = a;

console.log(formerB)
console.log(c);



回答2:


You can destructure the same property multiple times, onto different targets:

const { b: {c}, b: d } = a;

This assigns a.b.c to c and a.b to d.



来源:https://stackoverflow.com/questions/57065348/destructuring-and-rename-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!