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,229,940 Responses

  1. WilliamRhype表示:

    http://finasteride.store/# propecia tablets

  2. What Is Freezers Table Top And Why You Should Care high-quality freezers

  3. WilliamRhype表示:

    http://finasteride.store/# order propecia tablets

  4. RandallTaulp表示:

    buy cytotec pills: buy cytotec pills – buy cytotec over the counter

  5. WilliamRhype表示:

    https://cytotec.club/# cytotec abortion pill

  6. RandallTaulp表示:

    lisinopril 10 12.5 mg: lisinopril 5 mg tablet price in india – drug lisinopril 5 mg

  7. RandallTaulp表示:

    lisinopril generic 10 mg: prinivil 20 mg tablet – lisinopril 15 mg

  8. RandallTaulp表示:

    purchase cytotec: cytotec pills buy online – Misoprostol 200 mg buy online

  9. Ronaldpsymn表示:

    https://cytotec.club/# buy cytotec pills online cheap

  10. RandallTaulp表示:

    lisinopril tablet 40 mg: lisinopril 2 5 mg tablets – lisinopril 80 mg tablet

  11. 10 Things Everyone Makes Up About Double Glazed Window Leeds
    repairs to double glazed windows

  12. WilliamRhype表示:

    https://cytotec.club/# cytotec buy online usa

  13. WilliamRhype表示:

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

  14. WilliamRhype表示:

    https://ciprofloxacin.tech/# buy generic ciprofloxacin

  15. Jest ich kilka, wymienić tu można @VideoDownloader, @videodownloader, @videodownload3r
    i wiele innych.

  16. RandallTaulp表示:

    nolvadex 20mg: tamoxifen endometriosis – tamoxifen depression

  17. This combination medicine is used to relieve moderate to severe
    pain.

  18. RandallTaulp表示:

    does tamoxifen cause bone loss: tamoxifen lawsuit – how to lose weight on tamoxifen

  19. 7 Tricks To Help Make The Most Of Your Adult Mental Health
    Services online mental health assessments

  20. Ronaldpsymn表示:

    http://cytotec.club/# cytotec buy online usa

  21. Ronaldpsymn表示:

    http://nolvadex.life/# nolvadex online

  22. WilliamRhype表示:

    https://cytotec.club/# Abortion pills online

  23. Transfeer表示:

    It’s hard to find experienced people about this subject, but you sound like you know what you’re talking about!
    Thanks

  24. WilliamRhype表示:

    https://finasteride.store/# get propecia without rx

  25. 20 Double Glazed Windows Near Me Websites Taking The Internet By Storm Replace double glazing unit

  26. Davidtes表示:

    buy cytotec buy cytotec pills online cheap buy cytotec

  27. 10 Websites To Help You Become An Expert In Sofa Sale Futon couch bed

  28. Ronaldpsymn表示:

    https://nolvadex.life/# tamoxifen and bone density

  29. RandallTaulp表示:

    lexapro and tamoxifen: nolvadex pills – nolvadex 10mg

  30. Link Login Gotogel Tips To Relax Your Daily Lifethe One Link Login Gotogel Technique Every Person Needs To Be Able To link Login gotogel

發佈留言

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