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,466,854 Responses

  1. BradleyEdino表示:

    sweet bonanza oyna sweet bonanza oyna sweet bonanza

  2. Wallacefex表示:

    deneme bonusu veren casino siteleri: Deneme Bonusu Veren Siteler – deneme bonusu veren casino siteleri

  3. Do not miss out on the opportunity to uncover Washington DC like never prior to along with a private metropolitan area scenic tour private limo service

  4. Wallacefex表示:

    guvenilir slot siteleri: slot oyunlar? – az parayla cok kazandiran slot oyunlar?

  5. Minnie Smith表示:

    If you have not attempted a Brazilian steakhouse yet, you’re losing out! The limitless skewers of completely prepared meat are just divine. Take a look at my thoughts at brazilian bbq restaurant near me

  6. BradleyEdino表示:

    Casino Siteleri Canl? Casino Siteleri deneme bonusu veren casino siteleri

  7. LlprPaish表示:

    Real savings made if you buy on this site. Cheap zantac for nausea . Should I call my doctor?

  8. Because the admin of this web page is working, no doubt very shortly it will be well-known, due to its feature contents. http://Energypop.Co.kr/gnuboard5/bbs/board.php?bo_table=qna&wr_id=227889

  9. оплатить заказ можно при помощи банковской карты либо посредством прямого платежа в https://tesera.ru/user/gehthnfdt Индию.

  10. BradleyEdino表示:

    casino bahis siteleri casino bahis siteleri casino bahis siteleri

  11. Carl Flores表示:

    This was quite informative. For more, visit Mesa Roofing

  12. BradleyEdino表示:

    en guvenilir casino siteleri casino en iyi siteler guvenilir casino siteleri

  13. JasonUnsoG表示:

    http://denemebonusuverensiteler25.com/# deneme bonusu veren yeni siteler

  14. phising表示:

    Kalau ada pesan yang minta kamu klik link dan minta data, jangan langsung percaya.
    Bisa jadi itu cuma trik buat dapatin informasi pribadimu.
    Pastiin semuanya aman dulu!.

  15. Lucas Yates表示:

    I enjoyed this post. For additional info, visit skilled plastic surgeon

  16. Marion表示:

    If you would like to obtain a good deal from this article then you have to apply these strategies
    to your won website.

  17. JasonUnsoG表示:

    https://sweetbonanza25.com/# sweet bonanza kazanma saatleri

  18. BradleyEdino表示:

    deneme bonusu veren siteler deneme bonusu veren siteler yeni denemebonusuverensiteler25

  19. JasonUnsoG表示:

    http://slotsiteleri25.com/# guvenilir slot siteleri

  20. срочный вывод из запоя ростов http://vyvod-iz-zapoya-rostov229.ru/ .

  21. Wallacefex表示:

    sweet bonanza giris: sweet bonanza kazanma saatleri – sweet bonanza slot

  22. BradleyEdino表示:

    bilinmeyen bahis siteleri canl? casino siteleri casino bahis siteleri

  23. JasonUnsoG表示:

    https://sweetbonanza25.com/# sweet bonanza slot

  24. Superb, what a blog it is! This weblog provides useful data
    to us, keep it up.

  25. BrianBrevy表示:

    Avoid the hassle of airport parking with our convenient and affordable long-term parking options at O’Hare International Airport (ORD). Book online and enjoy a stress-free travel experience best long term parking at ord

  26. JasonUnsoG表示:

    https://sweetbonanza25.com/# sweet bonanza oyna

發佈留言

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