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

  1. BradleyEdino表示:

    deneme bonusu veren casino siteleri deneme bonusu veren casino siteleri guvenilir casino siteleri

  2. Josefa表示:

    Nine Things That Your Parent Teach You About Accident Attorneys In My
    Area accident attorneys (Josefa)

  3. FrankEmine表示:

    en cok kazand?ran slot oyunlar?: slot oyunlar? – slot siteleri

  4. BradleyEdino表示:

    slot oyunlar? slot oyunlar? puf noktalar? slot casino siteleri

  5. seobiawak表示:

    Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.

  6. tube sites表示:

    First off I would like to say terrific blog! I had a quick question in which I’d
    like to ask if you don’t mind. I was curious to know
    how you center yourself and clear your head before writing.
    I’ve had trouble clearing my mind in getting my ideas out there.
    I do take pleasure in writing however it just seems like the first 10 to
    15 minutes tend to be wasted simply just trying to
    figure out how to begin. Any suggestions or tips? Appreciate it!

  7. seobiawak表示:

    Terima kasih atas informasinya! Kebetulan saya menemukan pembahasan serupa di KADEKTOTO SLOT GACOR.

  8. kv-WOrk.COm表示:

    I’m gone to tell my little brother, that he should also pay
    a quick visit this web site on regular basis to obtain updated from most up-to-date reports. https://Kv-Work.com/bbs/board.php?bo_table=free&wr_id=1747206

  9. Your mode of explaining everything in this post is genuinely pleasant, all can simply be aware of it, Thanks a lot.

  10. seobiawak表示:

    Terima kasih atas informasinya! Kebetulan saya menemukan pembahasan serupa di KADEKTOTO SLOT GACOR.

  11. It’s hard finding trustworthy contractors; happy that I’ve found Home remodeling cypress tx

  12. seobiawak表示:

    Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.

  13. FrankEmine表示:

    slot oyunlar? puf noktalar?: slot casino siteleri – en kazancl? slot oyunlar?

  14. Dannyfut表示:

    darkmarkets how to get on dark web dark market onion

  15. seobiawak表示:

    Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.

  16. seobiawak表示:

    Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.

  17. seobiawak表示:

    ArtiVery interesting article! I also just read the related article on KADEKTOTO SLOT GACOR which discusses similar topics.

  18. yrtpjw表示:

    提供WPS下载,适配所有主流平台,包括Windows、Mac、Android和iOS。WPS Office提供全功能的文字处理、表格编辑、PPT制作和PDF管理工具,兼容Microsoft Office文件格式。免费下载WPS,享受高效便捷的办公体验,无论是个人用户还是团队协作,都能轻松应对。https://www.wps2.com/         rijpcuvgxm

  19. seobiawak表示:

    Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.

  20. These people were previously scheduled for tummy tuck prior to undergoing HIFU therapy.

  21. Jack Franklin表示:

    Thanks for the clear breakdown. Find more at adi yoga yoga instructor mumbai

  22. Medicament information sheet. Short-Term Effects.
    lisinopril hctz a 28
    Best trends of drugs. Read here.

  23. seobiawak表示:

    Pembahasan yang bagus! Untuk informasi tambahan, Anda bisa mengunjungi KADEKTOTO SLOT GACOR.

  24. Wallacefex表示:

    sweet bonanza: sweet bonanza – sweet bonanza

  25. Roxie Brock表示:

    Great insights into color choices for asphalt paving projects! Color can really change the aesthetic appeal of an area! Explore more at asphalt company jacksonville

  26. seobiawak表示:

    Terima kasih atas informasinya! Kebetulan saya menemukan pembahasan serupa di KADEKTOTO SLOT GACOR.

  27. seobiawak表示:

    Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.

發佈留言

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