数组对象去重,并统计重复数据出现得次数 1.需将json转换成js数组对象12345678910//var arr = JSON.parse(data)//若原数据不是json,此步骤不需要let arr = [ { name: "小红", id: 1 }, { name: "小橙", id: 1 }, { name: "小黄", id: 4 }, { name: "小绿", id: 3 }, { name: "小青", id: 1 }, { name: "小蓝", id: 4 }]; 2.在方法外定义hash数组,方便调用1234567891011121314151617181920212223242526272829var hash = [];function quchong(arr) { // 第一步,去重 // var hash = []; for (var i = 0; i < arr.length; i++) { for (var j = i + 1; j < arr.length; j++) { if (arr[i].id === arr[j].id) { ++i; j = i; } } arr[i].num = 0; hash.push(arr[i]); } // 第二步,统计重复个数 hash.forEach(item => { arr.forEach(dd => { if (item.id === dd.id) { item.num++ } }) }); return hash; console.log(hash) }quchong(arr)console.log(hash) #js 数组对象去重,并统计重复数据出现得次数 https://huangzunxue998.top/2022/12/12/数组对象去重/ Author 黄dada Posted on December 12, 2022 Licensed under 常用服务器命令 Previous 无缝滚动,兼容ie7 Next Please enable JavaScript to view the comments