我想从多个变量中创建一个对象,但我不想一一列出这些变量:
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 };
我认为没有第二种作业的较短方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句