Is there a way to unpack values from an array of objects using destructuring assigments?
[
{a : 1},
{a : 1},
{a : 1},
{a : 1}
]
The result I need here is an array: [1,1,1,1]
Destructuring that would require the creation of 4 separate variables which would then get recombined into an array later. It would be very WET, and wouldn't make much sense, but if you had to:
const arr = [
{a : 1},
{a : 1},
{a : 1},
{a : 1}
]
const [{ a: item1 }, { a: item2 }, {a: item3}, {a: item4 }] = arr;
const newArrOfAs = [item1, item2, item3, item4];
console.log(newArrOfAs);
Your original code using reduce is better, but even more appropriate would be to use Array.prototype.map, since the input array and output array's items are one-to-one:
const arr = [
{a : 1},
{a : 1},
{a : 1},
{a : 1}
]
const newArrOfAs = arr.map(({ a }) => a);
console.log(newArrOfAs);
You could use the upcoming Array#flatMap
var array = [{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }];
result = array.flatMap(Object.values);
console.log(result);