0

I want to create a new object based on anther object but with fewer properties.

I know I can do it by manually assigment like this:

const obj = {
  a: 1,
  b: 2,
  c: 3
};
const smallObj = {
  a: obj.a
};

console.log(smallObj)

Is there a way to do it with destructuring?

I have tried doing this:

const obj = {
  a: 1,
  b: 2,
  c: 3
};
const smallObj = {
  a
} = {...obj}

console.log(smallObj, a)

But as you can see, I get the variable a to be equal to 1 but smallObj is a reference to obj.

Sebastian Simon
  • 16,564
  • 7
  • 51
  • 69
Amit Wagner
  • 2,831
  • 3
  • 15
  • 31

0 Answers0