I have an object oldObj
that I would like to pick some properties from:
let oldObj = {a: 1, b: 2, c: {d: 3}, d: 100, e: 101}
I want to unpack the properties using the destructuring assignment to create a new object.
let {a, b, c: {d}} = oldObj
let convertedObj = {a,b,d} // {a: 1, b: 2, d: 3}
Is there a way to do this in one line? This would allow for unpacking into a new object without having to write each and every properties twice. E.g something like:
let convertedObj = {...({a,b,c: {d}} = oldObj)} // {a: 1, b: 2, d: 3}
The line above does not work, as the result of running ({a,b,c: {d}} = oldObj)
will return the whole oldObj
. Hence running ...
on that will unpack the whole object in its original form.