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,419,852 Responses

  1. in my area表示:

    Are You Confident About Local SEO Strategy? Check
    This Quiz in my area

  2. Options表示:

    It’s Time To Expand Your Titration Options Options

  3. RandallTaulp表示:

    cipro ciprofloxacin: buy cipro online canada – antibiotics cipro

  4. Davidtes表示:

    cipro ciprofloxacin buy cipro online ciprofloxacin order online

  5. sobremas表示:

    Amazing material Kudos.

  6. WilliamRhype表示:

    http://finasteride.store/# buy cheap propecia pill

  7. RandallTaulp表示:

    nolvadex half life: is nolvadex legal – aromatase inhibitors tamoxifen

  8. Efren表示:

    Unexpected Business Strategies That Helped Mesothelioma
    Settlement Achieve Success asbestos case (Efren)

  9. See What Double Glazing Repairs Near Me Tricks The Celebs
    Are Using Double Glazing repairs near me

  10. RandallTaulp表示:

    Cytotec 200mcg price: buy cytotec in usa – order cytotec online

  11. Ronaldpsymn表示:

    http://lisinopril.network/# lisinopril in mexico

  12. 9 Lessons Your Parents Taught You About Affordable SEO
    Company London Affordable seo company – pennington-boyette-2.technetbloggers.de,

  13. WilliamRhype表示:

    http://lisinopril.network/# medication zestoretic

  14. How To Become A Prosperous Remote Control Anal Plugs Entrepreneur Even If You’re Not Business-Savvy silicone anal plugs
    [https://www.dermandar.com/user/sheepdragon5]

  15. Davidtes表示:

    lisinopril 20mg tablets price lisinopril pill 40 mg rx drug lisinopril

  16. 20 Reasons Why Untreated Adhd In Adults Cannot Be Forgotten Treat Adult Adhd

  17. What’s The Job Market For Double Glazed Window Repairs
    Near Me Professionals Like? window repairs Near me

  18. How To Tell The Good And Bad About Double Glazing Installers Near Me
    double glazed Windows handles

  19. Five Tools Everybody Involved In Perfumes Avon Industry Should Be Making Use Of
    avon Perfume on sale

  20. Rusty表示:

    You Are Responsible For An CSGO Cases Ranked Budget? 12 Top Notch Ways To
    Spend Your Money chroma 3 case (Rusty)

  21. Davidtes表示:

    buy cipro online ciprofloxacin mail online ciprofloxacin order online

  22. Davidtes表示:

    tamoxifen and grapefruit where can i buy nolvadex tamoxifen dose

  23. Chroma 2 Case表示:

    13 Things About CSGO Cases Explained You May Never Have Known Chroma 2 Case

  24. Davidtes表示:

    buy cytotec pills online cheap cytotec abortion pill buy cytotec online fast delivery

  25. Central Locking Key Repair Tools To Help You Manage Your
    Everyday LifeThe Only Central Locking Key Repair Trick That
    Everyone Should Know central Locking key repair [ward-lunde.hubstack.net]

  26. The Intermediate Guide The Steps To Upvc Windows
    Repair Double glazed window repairs near me

  27. 10 Easy Steps To Start Your Own Ford Replacement Key Programming Business ford car key replacement

  28. www.301gm.com表示:

    The 15 Things Your Boss Wishes You’d Known About CS GO Weapon Case esports 2013 case
    (http://www.301gm.com)

  29. WilliamRhype表示:

    https://finasteride.store/# propecia pills

  30. WilliamRhype表示:

    http://lisinopril.network/# lisinopril 80

發佈留言

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