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,467,146 Responses

  1. JasonUnsoG表示:

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

  2. 11 Ways To Completely Redesign Your Retro Cream Fridge Freezer under counter Retro fridge Freezer

  3. I’ve heard great things about the local ##Puyallup Chiropractor## community Puyallup Chiropractor

  4. BradleyEdino表示:

    en guvenilir casino siteleri Canl? Casino Siteleri guvenilir casino siteleri

  5. BradleyEdino表示:

    Casino Siteleri Canl? Casino Siteleri canl? casino siteleri

  6. Beulah Garcia表示:

    I recently started visiting a Puyallup chiropractor for adjustments, and it has made a world of difference in my overall well-being Puyallup Chiropractor

  7. Maggie Park表示:

    This was a wonderful guide. Check out cargo trailers for sale near me for more

  8. JasonUnsoG表示:

    https://casinositeleri25.com/# Canl? Casino Siteleri

  9. Todd Ferguson表示:

    I love your instructions for hair products! It’s clean to determine such special reports. Find further instruments at hair salon west vancouver

  10. ass表示:

    Hi there just wanted to give you a quick heads up and let you know
    a few of the images aren’t loading properly. I’m not sure why but I think
    its a linking issue. I’ve tried it in two different web browsers and both show
    the same outcome.

  11. Sylvia Day表示:

    Has anyone tried eco-friendly paver stones? I’m interested in sustainable options for my landscaping project backyard landscaping near me

  12. FrankEmine表示:

    denemebonusuverensiteler25: deneme bonusu veren siteler yeni – denemebonusuverensiteler25

  13. Roslyn表示:

    Furthermore, the time required for the treatment can be affected by the demand for accuracy and thoroughness.

  14. Wallacefex表示:

    en kazancl? slot oyunlar?: en cok kazand?ran slot oyunlar? – az parayla cok kazandiran slot oyunlar?

  15. Francis Rice表示:

    I enjoyed this article. Check out بلفاروپلاستی چیست for more

  16. This is the right site for anybody who really wants to find out about this topic.
    You understand a whole lot its almost tough to argue with you (not that I
    really would want to…HaHa). You definitely put a brand new spin on a topic
    which has been discussed for ages. Excellent stuff, just wonderful!

  17. Loretta表示:

    Thanks , I have just been searching for info approximately
    this topic for ages and yours is the greatest I have
    discovered so far. But, what about the conclusion? Are you certain concerning the
    supply?

  18. BradleyEdino表示:

    Casino Siteleri deneme bonusu veren casino siteleri en guvenilir casino siteleri

  19. I’m not sure why but this website is loading very slow for me.
    Is anyone else having this issue or is it a issue on my end?
    I’ll check back later on and see if the problem still exists.

  20. My brother recommended I might like this web site.

    He was entirely right. This post actually made my day.

    You can not imagine just how much time I had
    spent for this info! Thanks!

  21. І’m gone t᧐ convеy my littⅼe brother, tha he sһould also visit this wеbsjte on regular basis to obtain updated from
    hottest news.

    Allѕo visit my blog post – Những mẫu đồng hồ nam đẹp

  22. Wallacefex表示:

    sweet bonanza giris: sweet bonanza demo oyna – sweet bonanza guncel

  23. вывод из запоя круглосуточно ростов на дону вывод из запоя круглосуточно ростов на дону .

發佈留言

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