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)

2

1 Answer 1

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)

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

1 Comment

You really posted a new 'group by' answer?

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.