javascript 比较数组中的重复对象,如果找到则合并

xqnpmsa8  于 2022-12-28  发布在  Java
关注(0)|答案(1)|浏览(137)

我有一个对象数组,其中有一些重复的id,每个对象都有不同的firstName lastName和userId,现在我尝试做的是检查重复项,如果找到,创建一个trailers数组,并将其推送到第一个重复项,到目前为止一切顺利,我设法编写了一个函数,下面是我添加的代码,任何改进代码的建议都非常感谢

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

0qx6xfy61#

不是对代码的改进,而是一种不同的方法
reduce实现。我用一个对象按id分组,最后用Object.values取对象的值数组。我还用了rest语法和解构属性来缩短东西。

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)

相关问题