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
  • 14,320
  • 6
  • 42
  • 61
Amit Wagner
  • 2,370
  • 3
  • 9
  • 28

0 Answers0