JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

1,251,557 Responses

  1. Thank you! Loads of material!

  2. Nikefut表示:

    drug markets onion blackweb official website darkmarkets

  3. What’s Happening i am new to this, I stumbled upon this I have discovered It positively helpful and it has helped me out
    loads. I hope to contribute & aid other customers like its aided me.
    Great job.

  4. BrandenSlomy表示:

    amoxicillin buy no prescription: Amoxicillin for sale – how to buy amoxicillin online

  5. Тут можно преобрести купить сейф оружейный сейф под ружье цена

  6. Bradleygoath表示:

    prednisone 30 mg coupon generic Prednisone 20mg prednisone

  7. Seven Reasons To Explain Why Biofuel Fire Is So
    Important Bio fuel fireplace

  8. Markdob表示:

    Всем привет! Подскажите, где почитатьразные статьи о недвижимости? Пока нашел – https://artpronn.ru

  9. Узнай все о чем опасно варикоцеле варикоцеле

  10. You need to take part in a contest for one of the greatest sites on the internet.
    I am going to recommend this website!

  11. Markdob表示:

    Всем привет! Подскажите, где почитатьразные статьи о недвижимости? Сейчас читаю – https://artpronn.ru

  12. BrandenSlomy表示:

    where can i get amoxicillin: Com Pharm – amoxicillin 500mg pill

  13. StevenNub表示:

    buy amoxicillin without prescription: amoxil – amoxicillin 500mg cost

  14. Узнай все о степени варикоцеле варикоцеле слева

  15. Yanyiku.Cn表示:

    A Step-By’-Step Guide For Cryptocurrency Online Casino Cryptocurrency Casino – Yanyiku.Cn

  16. StevenNub表示:

    amoxacillian without a percription: Amoxicillin for sale – amoxicillin capsules 250mg

  17. A Complete Guide To Mesothelioma From Asbestos mesothelioma lawsuit; http://www.bitsdujour.Com,

  18. вызов нарколога на дом круглосуточно вызов нарколога на дом круглосуточно .

  19. Hey just wanted to give you a quick heads up. The text in your
    article seem to be running off the screen in Ie. I’m not
    sure if this is a formatting issue or something to do with internet
    browser compatibility but I figured I’d post to
    let you know. The layout look great though!
    Hope you get the problem solved soon. Many thanks

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。