I have an object like so:
const obj = {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5
}
I want to create multiple object literals out of it. So:
const obj1 = {
a: 1,
b: 2
}
const obj2 = {
c: 3,
d: 4,
e: 5
}
Is it possible to achieve this using an object destructing assignment?
Something like:
const { {a, b}: obj1, {c, d, e}: obj2} = obj;
No, it is not possible. However, you can write a function to split an object by its property names into multiple objects like this:
By returning the sub-objects in a tuple, you have the freedom to destructure it, naming each one according to your preference.