简体   繁体   English

如何获取数组中的重复对象?

[英]How to get the duplicates objects in an array?

I have an array like this:我有一个这样的数组:

    var clients=[{"id":1,"name":"john","age":20},
{"id":3,"name":"dean","age":23},
{"id":12,"name":"harry","age":14},
{"id":1,"name":"sam","age":22},
{"id":13,"name":"Bolivia","age":16},
{"id":7,"name":"sabi","age":60},
{"id":7,"name":"sahra","age":40},
{"id":4,"name":"natie","age":53},{"id":7,"name":"many","age":22}]

I want to find the duplicate objects and cluster them like this:我想找到重复的对象并将它们聚集成这样:

 [
       {
       "id":1,
        "clients":[
                    {"id":1,"name":"john","age":20},
                    {"id":1,"name":"sam","age":22}
                   ]
       },
     {
       "id":7,
       "clients":[
                   {"id":7,"name":"sabi","age":60},
                   {"id":7,"name":"sahra","age":40},
                   {"id":7,"name":"many","age":22}
                  ]
      }
    ]

can I do that with filter() like this: clients.filter(//code hier) ?我可以像这样使用 filter() 来做到这一点: clients.filter(//code hier)吗?

reduce() is tailor made for this. reduce()就是为此量身定做的。 When you want to aggregate over an array and get a computed result, you should use reduce() .当你想对一个数组进行聚合并得到一个计算结果时,你应该使用reduce()

find() is another array method, which helps in finding an array element based on a condition (here the matching of id property). find()是另一种数组方法,它有助于根据条件查找数组元素(这里是 id 属性的匹配)。

 var clients=[{"id":1,"name":"john","age":20}, {"id":3,"name":"dean","age":23}, {"id":12,"name":"harry","age":14}, {"id":1,"name":"sam","age":22}, {"id":13,"name":"Bolivia","age":16}, {"id":7,"name":"sabi","age":60}, {"id":7,"name":"sahra","age":40}, {"id":4,"name":"natie","age":53},{"id":7,"name":"many","age":22}] let ans = clients.reduce((agg,x,index) => { let findI = agg.find( a => a.id === x.id ); if(findI) findI.clients.push(x); else { agg.push({ id: x.id, clients: [x] }); } return agg; },[]); console.log(ans);

The simplest solution would be to loop over the clients and check for an existing object with the same id .最简单的解决方案是遍历clients并检查具有相同id的现有 object 。 If yes, push to clients array.如果是,则推送到clients数组。 Or else, just create one.否则,只需创建一个。

 var clients = [{ "id": 1, "name": "john", "age": 20 }, { "id": 3, "name": "dean", "age": 23 }, { "id": 12, "name": "harry", "age": 14 }, { "id": 1, "name": "sam", "age": 22 }, { "id": 13, "name": "olivia", "age": 16 }, { "id": 7, "name": "sabi", "age": 60 }, { "id": 7, "name": "sahra", "age": 40 }, { "id": 4, "name": "natie", "age": 53 }, { "id": 7, "name": "kany", "age": 22 }] const groups = []; for (let client of clients) { const existingGroup = groups.find(group => group.id == client.id) if (existingGroup) existingGroup.clients.push(client); else { groups.push({ id: client.id, clients: [client] }); } } console.log(groups);

You can reassign the original object with the temporary object just used for this, and continue with your business logic, which I believe is the one you are looking for.您可以使用刚刚用于此目的的临时 object 重新分配原始 object,并继续您的业务逻辑,我相信这是您正在寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM