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,925 Responses

  1. FrankEmine表示:

    Canl? Casino Siteleri: guvenilir casino siteleri – Deneme Bonusu Veren Siteler

  2. Sallie Luna表示:

    Appreciate the comprehensive insights. For more, visit dr thakur dental clinic

  3. Hilda Mullins表示:

    Thanks for the helpful advice. Discover more at استخدام نیروی نمایشگاهی

  4. вывод из запоя на дому ростов круглосуточно [url=https://vyvod-iz-zapoya-rostov27.ru/]вывод из запоя на дому ростов круглосуточно[/url] .

  5. Rory表示:

    Way cool! Some extremely valid points! I appreciate you penning this write-up and also the rest of the site is also really
    good.

  6. 5 Trucking Lawyer Projects For Every Budget lawyers truck accident near me (lovewiki.faith)

  7. Randy Horton表示:

    This was a fantastic read. Check out mens salon near me for more

  8. Wallacefex表示:

    deneme bonusu veren siteler yeni: yeni deneme bonusu veren siteler – yat?r?ms?z deneme bonusu veren siteler

  9. Great insights! Discover more at رنگ ضد حریق

  10. I appreciated this post. Check out متن روز پدر برای استوری for more

  11. 10 Things You Learned In Preschool That’ll Help You Understand
    Folding Treadmill With Incline why is incline treadmill good –
    http://www.google.co.ls,

  12. JasonUnsoG表示:

    http://sweetbonanza25.com/# sweet bonanza guncel

  13. Holly表示:

    Undeniably believe that which you stated. Your favorite
    justification appeared to be on the net the simplest thing to
    be aware of. I say to you, I certainly get irked while people consider worries that they plainly do not know about.
    You managed to hit the nail upon the top and also defined out the whole thing without having side-effects , people
    can take a signal. Will probably be back to get more.
    Thanks

  14. Governor Brian Kemp is a staunch opponent of legalized gambling, which is why sports wagering hasn’t taken off in the Peach State.

  15. Myrtle Cain表示:

    This was quite enlightening. Check out spa denver for more

  16. Medicines information sheet. Brand names.
    can i get neurontin without a prescription
    Some what you want to know about medicines. Read information here.

  17. 김포호빠表示:

    여행 가기 전, 여행 후 저희 김포호빠에서 피로를 한 번 풀어 보시죠

  18. FrankEmine表示:

    casino bahis siteleri: Casino Siteleri – canl? casino siteleri

  19. Hi to every body, it’s my first visit of this web site; this blog carries amazing and actually
    excellent material in favor of readers.

  20. JacobLoody表示:

    http://www.sklad-ymov.ru — На нашем сайте вы найдете всё необходимое для создания кухни вашей мечты.

  21. Winifred Pope表示:

    I love how distinct your explanation of lock mechanisms is—very wonderful learn! Thanks! locksmith

  22. Dannyfut表示:

    tor market links dark web sites links dark web sites

  23. Terry Owen表示:

    Appreciate the great suggestions. For more, visit مقارنة بين الكازينوهات

  24. JasonUnsoG表示:

    https://casinositeleri25.com/# guvenilir casino siteleri

  25. JasonUnsoG表示:

    https://denemebonusuverensiteler25.com/# deneme bonusu veren siteler

  26. site here表示:

    Excellent article! We are linking to this particularly great article on our
    site. Keep up the great writing.

發佈留言

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