0

I want to create an object from multiple variables but i don't want to list these variables one by one:

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

Furthermore, i want to cut special values from one object and put them into another one with the same keys:

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

How can i do these things with ES6/7?

melpomene
  • 79,257
  • 6
  • 70
  • 127
mqklin
  • 1,729
  • 3
  • 17
  • 34
  • So do you have a list now or an object? – Bergi Nov 17 '15 at 04:16
  • 1
    Your question, or at least the second part of it, seems to be a duplicate of [One-liner to take some properties from object in ES6](http://stackoverflow.com/q/25553910/1048572) – Bergi Nov 17 '15 at 04:17

1 Answers1

2

For the first one, you can use this

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

I don't think there is a shorter way to do the second assignment.

Lucius
  • 2,752
  • 3
  • 18
  • 39