首页 > 解决方案 > Javascript - 如何破坏对象并克隆属性?

问题描述

我想破坏一个对象并克隆一个特定的属性,所有这些都在一行中。这是可能的?

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

标签: javascriptecmascript-6destructuring

解决方案


const MyObject = {
  sections: [1, 2],
  otherProp: null
}

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

推荐阅读