1

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}]
4
  • 2
    Assume it is possible, have a go, and then edit this question with a minimal reproducible example of that effort. Commented Jun 30, 2022 at 10:18
  • 1
    I would recommend reading about and trying: .reduce() Commented Jun 30, 2022 at 10:20
  • I think you may need to look at "sorting functions" but I'm not entirely clear on what you need as output. Commented Jun 30, 2022 at 10:21
  • 1
    @evolutionxbox - Ok, I can do this via for loop for each element but it wont be a generic solution for each properties. I will still give a try and post an update. Commented Jun 30, 2022 at 10:23

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was looking for something exactly like this. I didnt know Object.assign can merge the properties. I was doing it manually.
1

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))

2 Comments

Thanks, I am still trying to understand but it works!
Pure code answers without explanation is a real PITA, and doesn't really help anyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.