Sniberb
Sniberb3y ago

⛄ 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...]]
6 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Sniberb
Sniberb3y ago
That doesnt look very clean Is that the best way? I was trying to use reduce
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Sniberb
Sniberb3y 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
marz
marz3y 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
reactibot
reactibot3y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/986993215112642600