1

I want to do something like this. There is object like

obj = { a: "xxx", b: "xxx", c: "xxx", d: "xxx", e: "xxx", f: "xxx" }

There i want to remove the values of property c and d and to put them in array like arr = ["xxx" , "xxx"] . Then I want to add this array to the obj object as

obj = { a: "xxx", b: "xxx",  c: ["xxx", "xxx"], e: "xxx", f: "xxx" }

so is there way to this using ES6 spread and rest operators

How to do so if i have to remove values of n number(unknown it can be a 1 or 2 or 3 ..) properties and put them in array like above i have explained

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Jonny
  • 823
  • 3
  • 14
  • 25

1 Answers1

2

You could do it using destructuring and temporary consts.

const obj = {a:"xxx" , b:"xxx", c:"xxx" , d:"xxx" , e:"xxx", f:"xxx"}

const { c, d, ...rest} = obj; // pick props you want to remove and keep rest

// create a new object by spreading `rest` and adding new property
console.log({...rest, c: [c, d]});
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98