I have 3 array objects-
dates = [{date: '12/04/2023'}, {date: '13/04/2023'}]
days = [{day: 'Monday'}, {day: 'Tuesday'}]
time = [{time: '09-10'}, {time: '10-11'}]
the final array should be like:
final = [
{
date: '12/04/2023',
day: 'Monday',
time: '09-10'
},
{
date: '13/04/2023',
day: 'Tuesday',
time: '10-11'
}
]
I tried merging/concat the arrays and other solutions that I could find but couldn’t find anything closer to my desired result.
I know there have been multiple questions on merging array objects but I haven’t been able to find any solution that fits my requirement.
Any help would highly appreciated.Thanks.
5
Answers
Try this changes,
you can use
map
and the index to access other arrays.You can use
for loop
for simple reading and better performanceAlso… there is this post that might help you with repeated date and you wanted to make it unique.
Create New array from another array
Using a for loop.
I think @cmgchess has the best answer,
Just wanted to share an interesting solution for this
by using
Array.prototype.reduce()
and the remainder of the array’s index and the length of the array: