查数组中重复最多的字段
作者:互联网
Array.prototype.repeat = function(){ let shipObj = this.reduce((pre, cur)=>{ pre[cur] ? pre[cur]++ : pre[cur] = 1; return pre; }, {}) // console.log(arguments.length) // arguments let valList = Object.values(shipObj).sort((m,n)=>n-m); Object.keys(shipObj).forEach(key=>{ valList.some((val,i)=>{ if(shipObj[key] === val){ valList[i] = [key, shipObj[key]]; return true; } return false }) }) return valList; } var nn = ['Tiff', 'Alice', 'Tiff', 'Bruce', 'Alice', 'Bob']; var res = nn.repeat()
标签:pre,return,cur,重复,valList,key,数组,最多,shipObj 来源: https://www.cnblogs.com/liujinyu/p/12005575.html