js数组对象去重问题

给的数据格式如下:

https://img2.mukewang.com/5c2dca9b0001570602740465.jpg

要转变成如下的格式:

https://img2.mukewang.com/5c2dcaa900010eee02440293.jpg

怎么实现呢?憋了好长时间了...应该循环几次?

眼眸繁星
浏览 448回答 1
1回答

米琪卡哇伊

function filter(students) {&nbsp; &nbsp; var obj = {};&nbsp; &nbsp; var result = [];&nbsp; &nbsp; for (var i = 0, len = students.length; i < len; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var item = students[i];&nbsp; &nbsp; &nbsp; &nbsp; var groupid = item.groupid;&nbsp; &nbsp; &nbsp; &nbsp; var groupname = item.groupname;&nbsp; &nbsp; &nbsp; &nbsp; var id = groupid + groupname;&nbsp; &nbsp; &nbsp; &nbsp; if (!id) continue;&nbsp; &nbsp; &nbsp; &nbsp; var index = obj[id];&nbsp; &nbsp; &nbsp; &nbsp; var current;&nbsp; &nbsp; &nbsp; &nbsp; if (index) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[index].g_count++;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.push({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_id: groupid,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_name: groupname,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_count: 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj[id] = result.length - 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result;}filter(students);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript