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,142,847 Responses

  1. Promisec.net表示:

    The Most Pervasive Issues With Veterans Disability Compensation veterans disability law Firms (Promisec.net)

  2. casement表示:

    The Most Pervasive Problems With Repair Double Glazing casement

  3. CharlesDog表示:

    https://cialist.pro/# Tadalafil price

  4. ElmerWes表示:

    order viagra Sildenafil 100mg price buy Viagra online

  5. 15 Best Twitter Accounts To Find Out More About Autowatch Ghost 2 Installers Near Me ferrari Ghost installer

  6. This Is The Advanced Guide To Replacement Windows Leeds upvc Door repair meanwood

  7. You’ll Never Guess This London Online Mobile Shopping Sites’s Tricks london online mobile Shopping sites

  8. Three Reasons Why The Reasons For Your Cheap Online Grocery Shopping Uk Is Broken (And How To Fix It) Custom Wood Floor registers

  9. ElmerWes表示:

    Kamagra 100mg kamagra oral jelly buy kamagra online usa

  10. JamesNow表示:

    cheap kamagra: kamagra – sildenafil oral jelly 100mg kamagra

  11. What’s The Most Common Easy Work From Home Jobs Uk Debate Doesn’t Have To Be As Black Or White As You May Think
    work from home jobs No experience (pattern-wiki.win)

  12. My brother suggested I might like this web site. He was totally right. This post truly made my day. You cann’t imagine simply how much time I had spent for this info! Thanks!

  13. 11 Ways To Completely Revamp Your Online Shopping Websites For Clothes
    Bonus Eraser Colored Pencils

  14. JamesNow表示:

    cenforce.pro: buy cenforce – Cenforce 150 mg online

  15. Searching For Inspiration? Try Looking Up London Online Clothing Shopping Sites
    professional diving Fins atomic

  16. Syair Sdy表示:

    Excellent article. Keep writing such kind of info on your site.

    Im really impressed by it.
    Hello there, You’ve done a fantastic job. I will definitely digg it and personally suggest to my
    friends. I am confident they will be benefited from this site.

  17. Meagan表示:

    What’s The Job Market For Double Glazed Window Repairs
    Professionals? double glazed window repairs (Meagan)

  18. ElmerWes表示:

    п»їBuy generic 100mg Viagra online Buy Viagra online Cheap generic Viagra

  19. Are You Getting The Most Value From Your Columbia Mesothelioma Lawyer?
    Houston Mesothelioma Lawyer

  20. DavidOwelo表示:

    https://levitrav.store/# Levitra online USA fast

  21. ElmerWes表示:

    Viagra online price Cheapest place to buy Viagra Cheap Viagra 100mg

  22. The 3 Biggest Disasters In Repair Double Glazed Windows The
    Repair Double Glazed Windows’s 3 Biggest Disasters In History Window Repairs

  23. My developer is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the
    costs. But he’s tryiong none the less. I’ve been using Movable-type on numerous websites
    for about a year and am nervous about switching to another platform.
    I have heard fantastic things about blogengine.net.
    Is there a way I can import all my wordpress content into it?
    Any help would be really appreciated!

  24. GeorgeSep表示:

    cheapest cialis: buy cialis online – Buy Cialis online

  25. Skin So soft表示:

    Seven Reasons To Explain Why Skin So Soft Is Important Skin So soft

  26. CharlesDog表示:

    http://cenforce.pro/# cenforce.pro

  27. Refugio表示:

    Who’s The World’s Top Expert On London Online Clothing Shopping
    Sites? 2 Shelf Rubbermaid Transport Cart, Refugio,

  28. peatix.com表示:

    The Best Way To Explain Mini Key Fob Replacement To Your
    Boss replace mini key fob – peatix.com

  29. JamesNow表示:

    Vardenafil buy online: Levitra 10 mg best price – Levitra 20 mg for sale

  30. Asbestos Case表示:

    The Biggest Issue With Asbestos Lawsuits And How You Can Resolve It Asbestos Case

發佈留言

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