I have a set of arrays that have multiple bits of data that I want to arrange into a new array of data. This is what I have:
const dateArray = ["9/13/2022", "9/13/2022", "9/13/2022", "9/13/2022","9/13/2022"]
const timeArray = ["00:00", "00:01", "00:02", "00:03", "00:04"]
const integerArray = [1,2,3,4,5]
This is what I want:
{
row0: ["9/13/2022", "00:00", 1],
row1: ["9/13/2022", "00:01", 2],
row2: ["9/13/2022", "00:02", 3],
row3: ["9/13/2022", "00:03", 4],
row4: ["9/13/2022", "00:04", 5]
}
Thanks for any info you could provide, I've tried to do map, reduce and group but I haven't had exactly what I want to come back and I'm losing my mind keeping track of what I've tried.
CodePudding user response:
Using Array#reduce
:
const dateArray = ["9/13/2022", "9/13/2022", "9/13/2022", "9/13/2022","9/13/2022"]
const timeArray = ["00:00", "00:01", "00:02", "00:03", "00:04"]
const integerArray = [1,2,3,4,5]
const res = dateArray.reduce((acc, _, i, dates) => ({
...acc, [`row${i}`]: [dates[i], timeArray[i], integerArray[i]]
}), {});
console.log(res);