⛄ Snowberb ⛄ – 13-58 Jun 16

Having this array:
[{date: Date, value: number}, {date: Date, value: number}, {date: Date, value: number}...]
[{date: Date, value: number}, {date: Date, value: number}, {date: Date, value: number}...]
How can I transform that array into a new one with the following structure using .reduce?:
[[date, date, date...], [value, value, value...]]
[[date, date, date...], [value, value, value...]]
UU
Unknown User672d ago
S
Sniberb672d ago
That doesnt look very clean Is that the best way? I was trying to use reduce
UU
Unknown User672d ago
S
Sniberb672d ago
reduce returns a new array doesnt it? how would you do it with reduce? it has been a very long time without using reduce so I really do not know how to do it
M
marz672d ago
const output = input.reduce(
(acc, curr) => {
return [
[...acc[0], curr.date],
[...acc[1], curr.value],
];
},
[[], []]
);
const output = input.reduce(
(acc, curr) => {
return [
[...acc[0], curr.date],
[...acc[1], curr.value],
];
},
[[], []]
);
I got a version working with reduce, but to be honest I don't see anything wrong with the for loop version, it ended up quite similar
UU
Unknown User672d ago