gpt4 book ai didi

javascript - 从数组中返回值并按频率出现

转载 作者:行者123 更新时间:2023-11-30 10:08:08 24 4
gpt4 key购买 nike

<分区>

所以我有这个代码

Array.prototype.byCount= function(){
var itm, a= [], L= this.length, o= {};
for(var i= 0; i<L; i++){
itm= this[i];
if(!itm) continue;
if(o[itm]== undefined) o[itm]= 1;
else ++o[itm];
}
for(var p in o) a[a.length]= p;
return a.sort(function(a, b){
return o[b]-o[a];
});
}

source

这几乎是我所需要的,只是它不返回带有值的出现次数。

我试图重写它,但我总是在排序部分失败。

谢谢你的帮助

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com