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,230,120 Responses

  1. Hildred表示:

    Guide To Mesothelioma Law: The Intermediate Guide To Mesothelioma Law asbestos claim [Hildred]

  2. Joe表示:

    What You Need To Do On This Ford Keys Cut key fob replacement ford (Joe)

  3. So , You’ve Purchased Peugeot Boxer Key … Now What?
    peugeot key fob replacement

  4. Who’s The Top Expert In The World On Coffee Machine Espresso?
    Coffee for espresso machine

  5. Why You Should Focus On Improving Black Friday Sofa Sale sofas Vs couch

  6. What’s The Job Market For Nespresso Machine White Professionals?

    Nespresso Machine White – http://Www.Dermandar.Com

  7. 12 Facts About Auto Folding Mobility Scooter Near Me
    To Make You Think Smarter About Other People
    automatic folding scooter

  8. 10 . Pinterest Account To Be Following Nespresso Pixie Coffee Machine nespresso machine Price

  9. A Trip Back In Time What People Talked About Loveseat For Sale 20 Years Ago Corduroy couch (https://www.Mazafakas.com/)

  10. Clyde表示:

    See What Window Repair Leeds Tricks The Celebs
    Are Utilizing window repair leeds [Clyde]

  11. The 10 Most Terrifying Things About Walking Pads For Under Desk Walking Pads for Under desk

  12. Davidtes表示:

    buy cytotec online buy misoprostol over the counter buy cytotec pills

  13. WilliamRhype表示:

    http://nolvadex.life/# aromatase inhibitor tamoxifen

  14. How To Explain Bunk Beds For Children To Your Grandparents Bunk Beds Uk Sale

  15. dermandar.com表示:

    Where Will Double Glazing Deals Near Me Be One Year From Right Now?
    double glazing Window replacement (dermandar.com)

  16. You’ll Never Be Able To Figure Out This Sex Machine Online Store’s Secrets Sex Machine Online Store

  17. Nice blog here! Also your site loads up very fast! What web host are you using?
    Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol

  18. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 20 mg tablet

  19. Ronaldpsymn表示:

    http://nolvadex.life/# tamoxifen and antidepressants

  20. new表示:

    Fleshlights: What’s New? No One Is Discussing new

  21. Ten Bunk Beds That Will Actually Improve Your Life bunk bed sale uk (http://qooh.me/beetlejuice13)

  22. RandallTaulp表示:

    nolvadex for sale amazon: tamoxifen postmenopausal – nolvadex only pct

  23. Davidtes表示:

    buy cipro online without prescription cipro pharmacy cipro 500mg best prices

  24. Ꭲhanks for sharing your thougһts ᧐n diѕhount boat motors for sale.

    Regards

    Ⅿy homepage – tohatsu outboards for sale

  25. WilliamRhype表示:

    https://lisinopril.network/# cheapest price for lisinopril

  26. Davidtes表示:

    buy cipro online ciprofloxacin mail online where can i buy cipro online

  27. 15 Terms Everyone Who Works In Window Repair Industry Should Know
    window repairs (0522445518.Ussoft.kr)

  28. Lori表示:

    10 Facts About Mesothelioma Claim That Will Instantly Put
    You In The Best Mood mesothelioma law (Lori)

發佈留言

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