• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

169
Views
Merge array elements group by properties in Javascript

I have the following javascript object array:

       [ { "firstName": "x", "lastName": "y", "age": 10}, 
         { "firstName": "x", "lastName": "y", "height": 100},
         { "firstName": "x", "lastName": "y", "weight": 50},
         { "firstName": "a", "lastName": "b", "age": 11}, 
         { "firstName": "a", "lastName": "b", "height": 110},
         { "firstName": "a", "lastName": "b", "weight": 60},
         { "firstName": "m", "lastName": "n", "age": 12}, 
         { "firstName": "m", "lastName": "n", "height": 120},
         { "firstName": "m", "lastName": "n", "weight": 70}]

Is it possible to group by firstName and lastName such that it includes other properties of the object in the array?

Expected Array:

       [ { "firstName": "x", "lastName": "y", "age": 10, "height": 100, "weight": 50}, 
         { "firstName": "a", "lastName": "b", "age": 11, "height": 110, "weight": 60},
         { "firstName": "m", "lastName": "n", "age": 12, "height": 120, "weight": 70}]
about 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Concept

Prepare a result array. Iterate through all the data. Check if the firstName and lastName exist in the result array. If no, push it into the result array. If yes, merge the object to get the missing properties.

Code

const data =[ { "firstName": "x", "lastName": "y", "age": 10}, 
         { "firstName": "x", "lastName": "y", "height": 100},
         { "firstName": "x", "lastName": "y", "weight": 50},
         { "firstName": "a", "lastName": "b", "age": 11}, 
         { "firstName": "a", "lastName": "b", "height": 110},
         { "firstName": "a", "lastName": "b", "weight": 60},
         { "firstName": "m", "lastName": "n", "age": 12}, 
         { "firstName": "m", "lastName": "n", "height": 120},
         { "firstName": "m", "lastName": "n", "weight": 70}];
let result = [];
let found;
data.forEach(d => {
  found = false;
  result.forEach(r => {
    if (r.firstName === d.firstName && r.lastName === d.lastName){
      Object.assign(r, d);
      found = true;
    }
  });
  if (!found){
    result.push(d);
  }
});
console.log(result);

about 3 years ago · Juan Pablo Isaza Report

0

you can do something like this

const group = data => Object.values(data.reduce((res, {firstName, lastName, ...rest}) =>{
  const key = JSON.stringify({firstName, lastName})
  
  return {
    ...res,
    [key]: {firstName, lastName, ...(res[key] || {}), ...rest}
  }
}, {}))



const data = [ { "firstName": "x", "lastName": "y", "age": 10}, 
         { "firstName": "x", "lastName": "y", "height": 100},
         { "firstName": "x", "lastName": "y", "weight": 50},
         { "firstName": "a", "lastName": "b", "age": 11}, 
         { "firstName": "a", "lastName": "b", "height": 110},
         { "firstName": "a", "lastName": "b", "weight": 60},
         { "firstName": "m", "lastName": "n", "age": 12}, 
         { "firstName": "m", "lastName": "n", "height": 120},
         { "firstName": "m", "lastName": "n", "weight": 70}]
         
console.log(group(data))

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error