Transform multiple objects with identic property into single one object – Javascript

I couldn’t find a good example or any resources on how to actually transform multiple objects that contain the same property (fullName) in my case and how to make only 1 object with the following structure (in order to work on just 1 object instead of multiple)

The sample objects look like this:

{
  fullName: 'Reuben Daly',
  from: '2015-06-01',
  to: '2016-07-01',
  companyName: 'Mondelēz International'
}
{
  fullName: 'Reuben Daly',
  from: '2017-04-01',
  to: '2017-12-01',
  companyName: 'Redgate Software'
}
{
  fullName: 'Reuben Daly',
  from: '2013-10-01',
  to: '2014-01-01',
  companyName: 'Cambridge International Examinations'
}
...

and the goal is to create 1 object that is actually containing all of the values stored in 1, like this:

{
  fullName: 'Reuben Daly',
  pos1from: '2015-06-01',
  pos1to: '2016-07-01',
  pos1companyName: 'Mondelēz International',
  pos2from: '2017-04-01',
  pos2to: '2017-12-01',
  pos2companyName: 'Redgate Software'
  pos3from: '2013-10-01,
  pos3to: '2014-01-01',
  pos3companyName: 'Cambridge International Examinations'
  ...

}

I have been trying to achieve that with reduce() but as I’m having objects instead of arrays it won’t work. Been reading about the forEach() method but can’t find a good example that is actually doing something similar and I’ve stuck.

39 thoughts on “Transform multiple objects with identic property into single one object – Javascript”

  1. 395959 568106I always pay a visit to your blog and retrieve everything you post here but I never commented but today when I saw this post, I couldnt stop myself from commenting here. Wonderful write-up mate! 196429

    Reply