分解为两个单独的变量

时间:2019-09-19 16:02:04

标签: javascript ecmascript-6

是否有一种快速分解对象的方法,以便将其存储到两个不同的组中?例如:

const obj = {a: 1, b: 2, c:3, d: 4, e: 5};
const {a, b} = obj;
// store the rest of the properties that weren't destructed above
const {otherStuff} = obj;

一些注意事项:

  1. 我知道该对象需要具备的第一个属性。
  2. 我不知道其余没有被破坏的属性是什么。

任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:4)

const {a, b, ...otherStuff} = obj;

答案 1 :(得分:1)

您可以将解构与其他参数语法...一起使用,以在另一个变量中获取对象的其余部分:

const obj = {a: 1, b: 2, c:3, d: 4, e: 5};
const {a, b, ...otherStuff} = obj;

console.log(otherStuff);