Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merge array objects of different values in a new object

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

you can use map and the index to access other arrays.

const dates = [{date: '12/04/2023'}, {date: '13/04/2023'}]
const days = [{day: 'Monday'}, {day: 'Tuesday'}]
const time = [{time: '09-10'}, {time: '10-11'}]

const res = dates.map(({date},i) => ({date, day: days[i].day, time: time[i].time}))

console.log(res)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading