0

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.

ankitjt
  • 477
  • 1
  • 5
  • 15
  • 1
    why is 10-11 a day on 3rd array. also can use map and spread and access other arrays using index – cmgchess Apr 12 '23 at 06:30
  • 1
    sorry that was a typo , I have updated my question. – ankitjt Apr 12 '23 at 06:34
  • Does this answer your question? [Create an object from an array of keys and an array of values](https://stackoverflow.com/questions/39127989/create-an-object-from-an-array-of-keys-and-an-array-of-values) – jsejcksn Apr 26 '23 at 20:40

5 Answers5

2

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)
cmgchess
  • 7,996
  • 37
  • 44
  • 62
  • This works perfectly, would you able to explain the logic briefly. Thanks – ankitjt Apr 12 '23 at 06:42
  • @ankitjt which part you dont understand. map is used to transform an array. so here I'm transforming the dates array into an array of objects containing the other 2 fields as well. to get the other 2 fields i use the index which is available in map – cmgchess Apr 12 '23 at 06:47
  • @ankitjt hope this was clear. let me know if any other questions – cmgchess Apr 12 '23 at 08:02
0

Try this changes,

dates = [{date: '12/04/2023'}, {date: '13/04/2023'}]
days = [{day: 'Monday'}, {day: 'Tuesday'}]
time = [{time: '09-10'}, {time: '10-11'}]
    
const result = dates.map((d,index) => {
    d.day = days[index].day;
    d.time = time[index].time;
})

console.log(result)
Hetal N
  • 158
  • 8
  • Please don't misuse `map` when you're not using the array it creates. If you just want a loop, just use a loop (or `forEach`). More in my post [here](https://thenewtoys.dev/blog/2021/04/17/misusing-map/). (But instead of doing `[...dates]` and then `map`, you could correctly using `map` on `dates`...) It's also probably worth making sure the OP understands this *modifies* the objects in the `dates` array, rather than creating new objects. – T.J. Crowder Apr 12 '23 at 06:35
  • Yes, I checked your post and found much useful, Thank you for your suggestion :) I have updated my answer – Hetal N Apr 12 '23 at 06:40
  • You're still using `map` without using its return value. See cmgchess' answer for how you'd properly use `map`. – T.J. Crowder Apr 12 '23 at 07:33
0

You can use for loop for simple reading and better performance

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

combined = []

for(i = 0; i < dates.length; i++){
  combined.push({
  date: dates[i].date, 
  day: days[i].day, 
  time: time[i].time
  })
}

console.log(combined)

Also... there is this post that might help you with repeated date and you wanted to make it unique. Create New array from another array

Chan
  • 61
  • 8
0

Using a for loop.

const final = [];

for (let date = 0; date < dates.length; date++) {
  for (let day = 0; day < days.length; day++) {
    for (let t = 0; t < time.length; t++) {
      const tmp = {
        date: dates[date].date,
        day: days[day].day,
        time: time[t].time
      };
      result.push(tmp);
    }
  }
}

console.log(final);
0

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:

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 size = dates.length;
const res = [...dates, ...days, ...time].flat().reduce((acc, curr, index) => {
  acc[index % size] = {
    ...acc[index % size],
    ...curr
  }

  return acc;
}, []);

console.log(res);
zb22
  • 3,126
  • 3
  • 19
  • 34