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,456,433 Responses

  1. CurtisBam表示:

    Indian pharmacy that ships to usa: Indian Cert Pharm – Online pharmacy

  2. Brandonnot表示:

    safe canadian pharmacy: best rated canadian pharmacy – cheapest pharmacy canada

  3. CurtisBam表示:

    Indian Cert Pharm: Online pharmacy – IndianCertPharm

  4. How To Find The Perfect Asbestos Attorney Lawyer Mesothelioma On The Internet mesothelioma Lawsuit

  5. CurtisBam表示:

    indian pharmacy: Indian Cert Pharm – Indian Cert Pharm

  6. CurtisBam表示:

    mexican rx online: Mexican Easy Pharm – Mexican Easy Pharm

  7. Carrolleruck表示:

    https://mexicaneasypharm.com/# buying from online mexican pharmacy
    canadian pharmacy phone number

  8. Mat6tube表示:

    Mat6tube I enjoy your site, but you might want to review the spelling in a few posts. Some of them have noticeable spelling issues, which I find slightly irritating, though I’ll certainly return again.

  9. Your post on gutter cleaning is super helpful! I will share it with my neighbors in Amherst Gutter Cleaning Service

  10. I’ll immediately take hold of your rss as I can’t in finding your e-mail subscription hyperlink or e-newsletter service.

    Do you’ve any? Kindly allow me know in order that I
    could subscribe. Thanks.

  11. Kate Burns表示:

    Interesting perspective on how clean gutters contribute towards energy efficiency too!!!   # # a n y K e y w o r Gutter Cleaning

  12. CurtisBam表示:

    legitimate canadian pharmacy online: Canadian Md Pharm – canadian world pharmacy

  13. Medicament information. Long-Term Effects.
    where to buy cheap tadacip prices
    Actual what you want to know about drug. Read now.

  14. CurtisBam表示:

    pet meds without vet prescription canada: Canadian Md Pharm – canadian pharmacy tampa

  15. My brother suggested I might like this website. He was totally right.
    This post actually made my day. You cann’t imagine simply how
    much time I had spent for this information! Thanks! http://www.novello.jp/liner/joyful/joyful.cgi

  16. aquasculpt表示:

    I blog quite often and I seriously thank you for your information. Your article has truly peaked my interest.
    I am going to take a note of your website and keep checking
    for new information about once per week. I subscribed to your Feed too.

  17. The 10 Most Scariest Things About Best Brand For Refrigerator good fridge brand (Theflatearth.win)

  18. ssgame666表示:

    id=”firstHeading” class=”firstHeading mw-first-heading”>Search results

    Help

    English

    Tools

    Tools
    move to sidebar hide

    Actions

    General

  19. booster xt表示:

    My brother recommended I might like this website. He was once entirely right.

    This publish actually made my day. You cann’t
    believe just how much time I had spent for this information! Thank you!

  20. CurtisBam表示:

    Best online Indian pharmacy: Indian Cert Pharm – Indian pharmacy that ships to usa

  21. Brandonnot表示:

    Best Indian pharmacy: indian pharmacy – Indian Cert Pharm

  22. CurtisBam表示:

    Mexican Easy Pharm: medication from mexico pharmacy – Mexican Easy Pharm

發佈留言

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