Is it possible to unpack some of the keys of an object to a new object?
Let's say, I want to copy 3 of the keys (a
, b
, c
) from test
object to a new object (abc
). Below mention code will work.
const test = {a:1, b:2, c:3, d:4, e:5 };
const {a, b, c} = test;
const abc = { a, b, c, f: 6};
Is there any approach with which I can do it in a single statement?
There is one more approach.
const test = {a:1, b:2, c:3, d:4, e:5 };
const abc = { ...test, f: 6};
But with this approach, I would later have to delete the unwanted keys (d
, e
in my case).
(Best Case Solution: If we don't have to keep track of unwanted keys. There can be n number of unwanted keys.)