我是Jquery和Javascript的新手。有人可以幫助我用jquery排序根據發生次數(count)排列。我嘗試過各種排序方法,但都沒有工作。按Javascript數組中排序的次數(count)排序
我在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
我需要這樣
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}
你能告訴我們你的代碼這麼遠? – elclanrs
這些不同的方法是什麼?他們怎麼不工作? –
我希望下面的信息可能幫助你達到同樣的.. http://stackoverflow.com/questions/19464440/jquery-javascript-sort-array-by-highest-count – JayP