Reputation: 883
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
Upvotes: 1
Views: 4868
Reputation: 45121
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]});
Upvotes: 2