Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按频率从数组和事件返回值_Javascript_Sorting_Frequency - Fatal编程技术网

Javascript 按频率从数组和事件返回值

Javascript 按频率从数组和事件返回值,javascript,sorting,frequency,Javascript,Sorting,Frequency,所以我有这个密码 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(v

所以我有这个密码

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];
    });
}
Array.prototype.byCount=function(){
var itm,a=[],L=this.length,o={};

对于(var i=0;i这应该满足您的要求:

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]= {item: p, frequency: o[p]};
    return a.sort(function(a, b){
        return o[b.item]-o[a.item];
    });
}
产生:

[ { frequency: 4, item: "oranges" }, { frequency: 2, item: "bananas"}, {frequency: 1, item: "apples"} ]
编辑


正如Bergi在评论中指出的那样,做
返回o[b.item]-o[a.item];
完全是过于复杂了,毫无意义。
返回b.frequency-a.frequency;
会更好。

这应该可以满足您的要求:

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]= {item: p, frequency: o[p]};
    return a.sort(function(a, b){
        return o[b.item]-o[a.item];
    });
}
产生:

[ { frequency: 4, item: "oranges" }, { frequency: 2, item: "bananas"}, {frequency: 1, item: "apples"} ]
编辑


正如Bergi在评论中指出的那样,做
返回o[b.item]-o[a.item];
完全是过于复杂了,毫无意义。
返回b.frequency-a.frequency;
会更好。

您可以通过执行以下操作来包含频率并简化代码:

Array.prototype.byCount=function(){
var o={};
this.filter(函数(el){return el;}).forEach(函数(el){
o[el]=(o[el]| | 0)+1;
});
返回对象.keys(o).map(函数(键){
返回{key:key,出现次数:o[key]};
}).排序(功能(a、b){
返回b.事件-a.事件;
});
}

console.log(JSON.stringify([1,2,null,9,9,undefined,9,9,1,1,3,1,1,9,2].byCount());
您可以通过执行以下操作来包含频率并稍微简化代码:

Array.prototype.byCount=function(){
var o={};
this.filter(函数(el){return el;}).forEach(函数(el){
o[el]=(o[el]| | 0)+1;
});
返回对象.keys(o).map(函数(键){
返回{key:key,出现次数:o[key]};
}).排序(功能(a、b){
返回b.事件-a.事件;
});
}

console.log(JSON.stringify([1,2,null,9,9,undefined,9,9,1,1,3,1,1,9,2].byCount());
如果要将发生计数添加到值中,可以将它们放在一起:

return a.sort(function(a, b){
    return o[b]-o[a];
}).map(function(v) {
    return {value: v, count: o[v]}
});
或者立即将其放入阵列中,并调整比较功能:

for (var p in o)
    a.push({value: p, count: o[p]});
return a.sort(function(a, b){
    return a.count-b.count;
});

如果要将发生计数添加到值中,可以在末尾将它们放在一起:

return a.sort(function(a, b){
    return o[b]-o[a];
}).map(function(v) {
    return {value: v, count: o[v]}
});
或者立即将其放入阵列中,并调整比较功能:

for (var p in o)
    a.push({value: p, count: o[p]});
return a.sort(function(a, b){
    return a.count-b.count;
});

请告诉我们您试图重写它,然后我们将能够在排序部分帮助您。请告诉我们您试图重写它,然后我们将能够在排序部分帮助您。这是不必要的复杂。这几乎可以肯定是真的。我花了尽可能少的努力修改现有代码。它正在工作,只有一件事。Wh在排序过程之前,我应该如何删除所有出现1的项目?谢谢您可以在
a[a.length]之前使用
if(o[p]!=1)
将其从结果
a
中排除=…
这是因为排序函数应该是
b.frequency-a.frequency
这是不必要的复杂。几乎可以肯定。我花了最少的努力修改现有代码。它正在工作,只是一件事。在排序过程之前,我应该怎么做才能删除所有出现1的项?谢谢d在
a[a.length]=…
之前使用
if(o[p]!=1)
将它们从结果
a
中排除,这是因为排序函数应该是
b.frequency-a.frequency