【发布时间】:2020-03-05 14:14:37
【问题描述】:
我想在一行中破坏一个对象并克隆一个特定属性。有可能吗?
const MyObject = {
sections: [1, 2],
otherProp: null
};
const { sections } = MyObject; // Not a copy/clone of the array
const sectionsClone = { ...MyObject.sections }; // Works - But is not a destructuration
// Ideal scenario (I know this syntax has an error)
const { ...sections: myIdealScenario } = MyObject
【问题讨论】:
-
const { sections } = MyObject;之后,为什么不理想? -
这叫“去结构”和“去结构”
-
如果您想复制数组,请明确说明:
const sections = MyObject.sections.slice()
标签: javascript ecmascript-6 destructuring