[英]Javascript - How to destruct an object and clone a property?
我想在一行中破坏一个对象并克隆一个特定的属性。 这是可能的?
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 MyObject = {
sections: [1, 2],
otherProp: null
}
const { sections: [...sections] } = MyObject
我认为你只是想克隆对象中的数组,你不需要解构它:
const MyObject = {
sections: [1, 2],
otherProp: null
};
const myIdealScenario = [...MyObject.sections];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.