0

Let's say I have an object with many props, and I am only interested in props 1,2,3

const { prop1, prop2, prop3, ...rest } = MyObj; //destructure, I know this much.

How do I build a new obj with only those 3 properties using spread, or destructure, or whatever;

the Long way

const myNewObj = {
    prop1: prop1,
    prop2: prop2,
    prop3: prop3
}

The fancy ES6 way ?

joedotnot
  • 4,810
  • 8
  • 59
  • 91

1 Answers1

2

Just a tiny bit smaller that your long way:

const { prop1, prop2, prop3, ...rest } = MyObj;

const myNewObj = { prop1, prop2, prop3 };

enter image description here

Paul-Louis Mas
  • 429
  • 3
  • 8