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...

884,261 Responses

  1. RandallTaulp表示:

    benefits of tamoxifen: buy nolvadex online – tamoxifen premenopausal

  2. Davidtes表示:

    Misoprostol 200 mg buy online cytotec pills buy online cytotec buy online usa

  3. It’s The Myths And Facts Behind Double Glazing Glass Replacement Near Me Double glased window

  4. It is in point of fact a great and useful piece of info.

    I am happy that you shared this useful info with us.
    Please keep us up to date like this. Thank you for sharing.

  5. You’ll Never Guess This Audi Car Key Replacement’s Tricks Audi Car Key Replacement

  6. Davidtes表示:

    get propecia pills buying generic propecia without rx cost of propecia pills

  7. See What Audi Replacement Key Service Near Me Tricks The Celebs Are Utilizing Audi replacement key service near me

  8. 5 Ghost Installation Projects For Any Budget ford focus ghost Installer

  9. 15 Reasons Not To Ignore Sash Window Repair double glazing repairs near me (valetinowiki.racing)

  10. MichaelZek表示:

    упаковка товара для маркетплейсов москва https://24fulfilment-marketplace.ru/

  11. cabemanis88表示:

    There is certainly a lot to find out about this issue.
    I love all of the points you’ve made.

  12. Auto Door Lock Repair: 10 Things I Wish I’d Known Sooner Electronic lock repair Near me

  13. Laurene表示:

    10 Things We Hate About Online Shop 60Lb French Cleat Hanger (Laurene)

  14. Davidtes表示:

    buy generic ciprofloxacin buy cipro cheap ciprofloxacin generic price

  15. 7 Simple Secrets To Completely Doing The Double Glazed Window Repair Near Me cheap double glazing

  16. Davidtes表示:

    benefits of tamoxifen does tamoxifen cause menopause how does tamoxifen work

  17. Corazon表示:

    15 Of The Most Popular Pinterest Boards Of All Time About Double Glazing Companies Near Me double glaxing (Corazon)

  18. Ronaldpsymn表示:

    http://lisinopril.network/# lisinopril 2016

  19. A Productive Rant Concerning Double Glazing Repairs Near Me double glazing near me

  20. 10 Websites To Help You Develop Your Knowledge About Double Glazing Firms Near Me Upvc double glazing windows

  21. Davidtes表示:

    tamoxifenworld tamoxifen generic nolvadex 10mg

  22. RandallTaulp表示:

    buy ciprofloxacin: ciprofloxacin – ciprofloxacin order online

  23. Paula表示:

    A Brief History Of Best SEO Agency Uk History Of Best SEO Agency Uk professional
    seo agency (Paula)

  24. RandallTaulp表示:

    propecia without insurance: cost propecia no prescription – propecia order

  25. What A Weekly Glazing Repairs Near Me Project Can Change Your Life double Glazing Repairs

  26. What’s The Job Market For Treadmills Near Me Professionals
    Like? treadmills near me (https://Gokseong.multiiq.com)

  27. Links表示:

    The Most Convincing Proof That You Need GSASER
    Links

  28. bos domino表示:

    Hey very nice blog!

  29. WilliamRhype表示:

    https://cytotec.club/# buy cytotec online fast delivery

  30. ide777表示:

    Thanks a bunch for sharing this with all folks you actually realize what you
    are talking approximately! Bookmarked. Please also seek advice from
    my website =). We can have a link trade contract among us

發佈留言

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