0

I have 1 array of objects with some duplicates id's, each object has a different firstName lastName and userId, now what I'm trying to do is check the duplicate if found and create an array of trainers and push it to the first duplicate entry, so far so good, I managed to write a function, here below I'm adding my code, any suggestion for improving the code is really appreciated

let data = [
    {
        "id": 1,
        "details": {
            "title": "x detail"
        },
        "userId": 146,
        "firstName": "me",
        "lastName": "testing"
    },
    {
        "id": 2,
        "details": {
            "title": "x detail"
        },
        "userId": 151,
        "firstName": "me",
        "lastName": "testing1"
    },
    {
        "id": 1,
        "details": {
            "title": "x detail"
        },
        "userId": 145,
        "firstName": "me",
        "lastName": "testing2"
    },
    {
        "id": 3,
        "details": {
            "title": "x detail"
        },
        "userId": 151,
        "firstName": "me",
        "lastName": "testing3"
    },
    {
        "id": 4,
        "details": {
            "title": "x detail"
        },
        "userId": 44,
        "firstName": "me",
        "lastName": "testing4"
    },
    {
        "id": 1,
        "details": {
            "title": "x detail"
        },
        "userId": 32,
        "firstName": "me",
        "lastName": "testing5"
    }
];


const dupes = []
const nonDupe = [];
    data.filter((o, index) => {
          if(dupes.find(i => i.id === o.id)) {
              o['trainers'] = [{
              firstName:o.firstName,
              lastName:o.lastName,
              id:o.userId
            }]
            delete o.firstName;
            delete o.lastName;
            delete o.userId;
            nonDupe.push(o)
            return true
          }
            o['trainers'] = [{
              firstName:o.firstName,
              lastName:o.lastName,
              id:o.userId
            }]
            delete o.firstName;
            delete o.lastName;
            delete o.userId;
            dupes.push(o)
          return false;
        })

        let sortWithTrainers = [];
        dupes.map((val1, id1) =>{
          nonDupe.map((val2, id2) =>{
              if(val1.id == val2.id){
                let merged = [...val1.trainers, ...val2.trainers]
                val1.trainers = merged
                sortWithTrainers.push(val1)
            }else{
              sortWithTrainers.push(val1)
            }
          })
        })

        let nonDupeFinal = [];
        sortWithTrainers.map((val) =>{
          if(nonDupeFinal.find(i => i.id === val.id)) {
            return true
          }
          nonDupeFinal.push(val)
          return false;
        })
        console.log(nonDupeFinal)
CodeBug
  • 1,649
  • 1
  • 8
  • 23
  • 3
    Does this answer your question? [Merge duplicate objects in array of objects](https://stackoverflow.com/questions/30025965/merge-duplicate-objects-in-array-of-objects) – Brewal Dec 27 '22 at 10:35
  • also [Group array items using object](https://stackoverflow.com/questions/31688459/group-array-items-using-object) – pilchard Dec 27 '22 at 11:11

1 Answers1

1

Not an improvement to your code but a different approach

Implementation using reduce. I use an object to group by the id and in the end take the values array of the object using Object.values. I also used the rest syntax and destructuring properties to shorten stuff.

let data = [    {        "id": 1,        "details": {            "title": "x detail"        },        "userId": 146,        "firstName": "me",        "lastName": "testing"    },    {        "id": 2,        "details": {            "title": "x detail"        },        "userId": 151,        "firstName": "me",        "lastName": "testing1"    },    {        "id": 1,        "details": {            "title": "x detail"        },        "userId": 145,        "firstName": "me",        "lastName": "testing2"    },    {        "id": 3,        "details": {            "title": "x detail"        },        "userId": 151,        "firstName": "me",        "lastName": "testing3"    },    {        "id": 4,        "details": {            "title": "x detail"        },        "userId": 44,        "firstName": "me",        "lastName": "testing4"    },    {        "id": 1,        "details": {            "title": "x detail"        },        "userId": 32,        "firstName": "me",        "lastName": "testing5"    }];

const res = Object.values(data.reduce((acc,{id, details,userId, ...rest}) => {
  acc[id] = acc[id] || {details,id,trainers:[]}
  acc[id].trainers.push({id:userId,...rest})
  return acc
},{}))

console.log(res)
cmgchess
  • 7,996
  • 37
  • 44
  • 62
  • You really posted a new ['group by'](https://stackoverflow.com/questions/31688459/group-array-items-using-object) answer? – pilchard Dec 27 '22 at 11:09