Javascript 使用与ES6/7中变量名称相同的键从变量创建对象

Javascript 使用与ES6/7中变量名称相同的键从变量创建对象,javascript,ecmascript-6,Javascript,Ecmascript 6,我想从多个变量创建一个对象,但不想逐个列出这些变量: let [x, y, z] = [1, 2, 3]; let obj = ??? // something shorter than {x: x, y: y, z: z}; obj.x === 1; // i want true here obj.y === 2; // i want true here obj.z === 3; // i want true here 此外,我想从一个对象中剪切特殊值,并使用相同的键将它们放入另一个对象中:

我想从多个变量创建一个对象,但不想逐个列出这些变量:

let [x, y, z] = [1, 2, 3];
let obj = ??? // something shorter than {x: x, y: y, z: z};
obj.x === 1; // i want true here
obj.y === 2; // i want true here
obj.z === 3; // i want true here
此外,我想从一个对象中剪切特殊值,并使用相同的键将它们放入另一个对象中:

let obj1 = {
  subobj1: {
    x: 1,
    y: 2,
    z: 3
  }
};
let obj2 = ??? // something shorter than {x: obj1.subobj1.x, y: obj1.subobj1.y,};
obj2.x === 1; // i want true here
obj2.y === 2; // i want true here
typeof obj2.z === "undefined"; // i want true here

如何使用ES6/7实现这些功能?

对于第一个,您可以使用

let [x, y, z] = [1, 2, 3];
let obj = { x, y, z };

我认为没有比完成第二项作业更短的方法了。

那么你现在有一个列表还是一个对象?你的问题,或者至少是问题的第二部分,似乎是重复的