Javascript - 如何销毁一个对象并克隆一个 属性?

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 myIdealScenario = [...MyObject.sections];
const MyObject = {
  sections: [1, 2],
  otherProp: null
}

const { sections: [...sections] } = MyObject