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

标签 javascript ecmascript-6 destructuring

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

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

关于Javascript - 如何销毁对象并克隆属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60547335/

相关文章:

javascript if 语句语法(需要帮助)

javascript - (WordPress/Ajax) 引用错误 : Can't find variable: ajaxobject

浏览器上的 JavaScript 模块

reactjs - 为什么解构可以与事件配合使用,而没有事件就无法进行解构

javascript - JS - 通过只选择你需要的来解构

javascript - 使用数组解构赋值调用哪个函数?

javascript - 使用ajax执行php插入功能

javascript - 在 javascript 中实现时显示 mouse_id 未定义的错误

JavaScript ES6 : unable to change value of button with ajax (with the rest of the js code working fine)

javascript - Object.assign 和 ... spread 运算符失败无提示,不添加元素