Is there any way to destructure an object and assign its properties into an array rather than as variables? For example:
const obj = { a: 1, b: 2 };
const { a, b } = obj;
const arr = [a, b];
This works, but it requires restating the variables as the array values.
I have tried:
const arr = [(const { a, b } = obj)];
but this is a syntax error.
I had a similar idea with Object.values as in
const arr = Object.values((const { red } = chalk));`
...but this has the same issue of not being able to do destructuring in expressions.
[obj.a, obj.b]?obj.a lot if I have a lot of properties to destructure.const arr = Object.values((const { red } = chalk));why notconst arr = Object.values(chalk.red)?const { a, b } = obj, arr = [ a, b ];. You still have to type the names twice, though, and it still introduces two new variables into the local scope.