按Javascript数组中的出现次数(计数)排序
我不熟悉Jquery和Javascript。有人能帮我根据数组中的出现次数(计数)进行Jquery排序吗。我尝试了各种排序方法,但都不起作用 我有一个Javascript数组,它是按Javascript数组中的出现次数(计数)排序,javascript,jquery,arrays,sorting,Javascript,Jquery,Arrays,Sorting,我不熟悉Jquery和Javascript。有人能帮我根据数组中的出现次数(计数)进行Jquery排序吗。我尝试了各种排序方法,但都不起作用 我有一个Javascript数组,它是 allTypesArray = ["4", "4","2", "2", "2", "6", "2", "6", "6"] // here 2 is printed four times, 6 is printed thrice, and 4 is printed twice 我需要这样的输出 newTypesA
allTypesArray = ["4", "4","2", "2", "2", "6", "2", "6", "6"]
// here 2 is printed four times, 6 is printed thrice, and 4 is printed twice
我需要这样的输出
newTypesArray = ["2","6","4"]
我试过了
function array_count_values(e) {
var t = {}, n = "",
r = "";
var i = function (e) {
var t = typeof e;
t = t.toLowerCase();
if (t === "object") {
t = "array"
}
return t
};
var s = function (e) {
switch (typeof e) {
case "number":
if (Math.floor(e) !== e) {
return
};
case "string":
if (e in this && this.hasOwnProperty(e)) {
++this[e]
} else {
this[e] = 1
}
}
};
r = i(e);
if (r === "array") {
for (n in e) {
if (e.hasOwnProperty(n)) {
s.call(t, e[n])
}
}
}
return t
}
6: 3
}
输出为
{4:2,2:6,6:3}
我不认为一步就有一个直接的解决方案,当然这不仅仅是一个排序(排序不会删除元素)。一种方法是构建对象的中间映射来存储计数:
var allTypesArray = ["4", "4","2", "2", "2", "6", "2", "6", "6"];
var s = allTypesArray.reduce(function(m,v){
m[v] = (m[v]||0)+1; return m;
}, {}); // builds {2: 4, 4: 2, 6: 3}
var a = [];
for (k in s) a.push({k:k,n:s[k]});
// now we have [{"k":"2","n":4},{"k":"4","n":2},{"k":"6","n":3}]
a.sort(function(a,b){ return b.n-a.n });
a = a.map(function(a) { return a.k });
注意,这里不需要jQuery。当您不操纵DOM时,您很少需要它。只是添加我的想法(有点太晚了)
var allTypesArray=[“4”、“4”、“2”、“2”、“2”、“6”、“2”、“6”、“6”];
var map=allTypesArray.reduce(函数(p,c){
p[c]=(p[c]| | 0)+1;
返回p;
}, {});
var newTypesArray=Object.keys(map).sort(函数a,b){
返回映射[b]-map[a];
});
log(newTypesArray)
我认为这里不需要jquery
这个问题已经有了好几个很好的答案,但我发现在某些浏览器中可靠性是一个问题(即Safari 10——尽管可能还有其他浏览器)
解决这一问题的一种有点丑陋但似乎可靠的方法如下:
function uniqueCountPreserve(inputArray){
//Sorts the input array by the number of time
//each element appears (largest to smallest)
//Count the number of times each item
//in the array occurs and save the counts to an object
var arrayItemCounts = {};
for (var i in inputArray){
if (!(arrayItemCounts.hasOwnProperty(inputArray[i]))){
arrayItemCounts[inputArray[i]] = 1
} else {
arrayItemCounts[inputArray[i]] += 1
}
}
//Sort the keys by value (smallest to largest)
//please see Markus R's answer at: http://stackoverflow.com/a/16794116/4898004
var keysByCount = Object.keys(arrayItemCounts).sort(function(a, b){
return arrayItemCounts[a]-arrayItemCounts[b];
});
//Reverse the Array and Return
return(keysByCount.reverse())
}
试验
这是我用来做这类事情的函数:
function orderArr(obj){
const tagsArr = Object.keys(obj)
const countArr = Object.values(obj).sort((a,b)=> b-a)
const orderedArr = []
countArr.forEach((count)=>{
tagsArr.forEach((tag)=>{
if(obj[tag] == count && !orderedArr.includes(tag)){
orderedArr.push(tag)
}
})
})
return orderedArr
}
Set
对象是值的集合。集合中的值只能出现一次;它在集合
的集合中是唯一的
singles
变量使用Set
对象将allTypesArray
中的所有唯一值进行排列,并在数组中使用排列运算符
sortedSingles
变量通过比较数字按升序对singles
数组的值进行排序。到目前为止,您能给我们看一下您的代码吗?这些方法有哪些?他们是怎么不起作用的?我希望下面的帖子能帮助你实现同样的目标。。这不是排序(因为您更改了数组的内容)@gabykag.Petrioli,是的,它是根据每个元素的出现次数进行排序。FWIW您可以保存几个字符返回m[v]=++m[v]| 0,m
谢谢。你救了我。:)@埃尔克兰斯我看起来像是在打高尔夫球吗^^OP希望根据每个值在原始数组中出现的数目进行排序。
function orderArr(obj){
const tagsArr = Object.keys(obj)
const countArr = Object.values(obj).sort((a,b)=> b-a)
const orderedArr = []
countArr.forEach((count)=>{
tagsArr.forEach((tag)=>{
if(obj[tag] == count && !orderedArr.includes(tag)){
orderedArr.push(tag)
}
})
})
return orderedArr
}
const allTypesArray = ["4", "4","2", "2", "2", "6", "2", "6", "6"]
const singles = [...new Set(allTypesArray)]
const sortedSingles = singles.sort((a,b) => a - b)
console.log(sortedSingles)