Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
Merge array of objects within array of objects

I have the following objects

Person {
   name: string
   birthday: Date
   lifeEvents: LifeEvent[]
}

LifeEvent {
   eventId: number
   message: string
   comments: string
}

As the data comes in chunks, I will have an array of Person with one object that has name and birthday with values but lifeEvents is empty (Think of this one like a parent object.)

All other objects won't have birthday and name populated and will have only one LifeEvent with either eventId and message with values or eventId and comments

Within that array, I need to get the parent object, which has name and birthday with values, then get all lifeEvents from the remaining objects, merge all items that contains same eventId into a LifeEvent then push it to lifeEvents of the parent.

I have tried array.reduce, array.map but can't figure out a way of combining those objects into one.

My output should be only one Person with all lifeEvents merged by eventId

Sample data:

let results = [
{ 
   name: 'Test1',
   birthday: '2022-06-14',
   lifeEvents: null
},
{ 
   name: null,
   birthday: null,
   lifeEvents: [
        {
           eventId: 1,
           message: 'First event',
           comments: null
        }
   ]
},
{ 
   name: null,
   birthday: null,
   lifeEvents: [
        {
           eventId: 2,
           message: 'Second event',
           comments: null
        }
   ]
},
{ 
   name: null
   birthday: null
   lifeEvents: [
        {
           eventId: 1,
           message: null,
           comments: 'First event comment'
        }
   ]
},
{ 
   name: null
   birthday: null
   lifeEvents: [
        {
           eventId: 2,
           message: null,
           comments: 'Second event comment'
        }
   ]
},
]

Appreciate any help.

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

The following produces the requested result based on examples provided:

let results = [
  { 
     name: 'Test1',
     birthday: '2022-06-14',
     lifeEvents: null
  },
  { 
     name: null,
     birthday: null,
     lifeEvents: [
          {
             eventId: 1,
             message: 'First event',
             comments: null
          }
     ]
  },
  { 
     name: null,
     birthday: null,
     lifeEvents: [
          {
             eventId: 2,
             message: 'Second event',
             comments: null
          }
     ]
  },
  { 
     name: null,
     birthday: null,
     lifeEvents: [
          {
             eventId: 1,
             message: null,
             comments: 'First event comment'
          }
     ]
  },
  { 
     name: null,
     birthday: null,
     lifeEvents: [
          {
             eventId: 2,
             message: null,
             comments: 'Second event comment'
          }
     ]
  },
];

// extract parent
const parentResult = results.find((result) => result.name);

// generate unique events from sibling entities
const uniqueEvents = new Map();
results.forEach((result) => result.lifeEvents?.forEach(
  (lifeEvent) => {
    if (uniqueEvents.has(lifeEvent.eventId)) {
      updateEvent(lifeEvent);
    } else {
      uniqueEvents.set(lifeEvent.eventId, { eventId: lifeEvent.eventId, message: lifeEvent.message, comments: lifeEvent.comments});
    }
  })
);

// function to update event that is already stored in uniqueEvents
function updateEvent(lifeEvent) {
  const existingLifeEvent = uniqueEvents.get(lifeEvent.eventId);
  if (lifeEvent.message) existingLifeEvent.message = lifeEvent.message;
  if (lifeEvent.comments) {
    if (existingLifeEvent.comments) {
      existingLifeEvent.comments.concat(lifeEvent.comments)
    } else {
      existingLifeEvent.comments = lifeEvent.comments;
    }
  }
}

// populate lifeEvents inside the parentResult
parentResult.lifeEvents = [];
uniqueEvents.forEach((uniqueId) => {
  parentResult.lifeEvents.push(uniqueId);
});

console.log(parentResult);

about 4 years ago · Santiago Gelvez Report

0

Premise: The data structure you are using is wrong, you should try to use arrays with homogenous models.

That said I used a reduce method, with a condition to treat the first element in a different way from the other ones.

Last thing, you said merge lifeEvents, I assume you meant to overwrite the nullish values for events with same ids, if you want to overwrite all values then you can omit the merge utility function I wrote.

let results = [{
    name: 'Test1',
    birthday: '2022-06-14',
    lifeEvents: null,
  },
  {
    name: null,
    birthday: null,
    lifeEvents: [{
      eventId: 1,
      message: 'First event',
      comments: null,
    }, ],
  },
  {
    name: null,
    birthday: null,
    lifeEvents: [{
      eventId: 2,
      message: 'Second event',
      comments: null,
    }, ],
  },
  {
    name: null,
    birthday: null,
    lifeEvents: [{
      eventId: 1,
      message: null,
      comments: 'First event comment',
    }, ],
  },
  {
    name: null,
    birthday: null,
    lifeEvents: [{
      eventId: 2,
      message: null,
      comments: 'Second event comment',
    }, ],
  },
];

const merge = (o1, o2) => {
  const r = {...o1}
  Object.keys(o1).forEach(k => r[k] = o2[k] || o1[k])
  return r
}

const r = results.reduce((o, curr, i) => {
  if (i === 0) {
    return { ...o,
      lifeEvents: []
    };
  } else {
    const currentEvent = curr.lifeEvents[0]
    const idx = o.lifeEvents.findIndex((_o) => _o.eventId === currentEvent.eventId);
    if (idx !== -1) return { ...o,
      lifeEvents: o.lifeEvents.map((_o, i) => i === idx ? merge(_o, currentEvent) : _o)
    }
    else return { ...o,
      lifeEvents: [...o.lifeEvents, currentEvent]
    }
  }
}, results[0]);

console.log("RESULT:", r);

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!