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,213 Responses

  1. mspeech.Kr表示:

    What’s The Job Market For Treadmill Desk Uk Professionals?
    treadmill desk Uk (mspeech.Kr)

  2. Rickysem表示:

    buy medicines online in india buy medicines from India indian pharmacies safe

  3. Michaelcew表示:

    http://indiaph24.store/# buy prescription drugs from india

  4. Michaelcew表示:

    http://indiaph24.store/# indian pharmacy

  5. You definitely made your point!

  6. Five Things You’ve Never Learned About Birth Injury Settlement birth injury lawyers

  7. JustinCit表示:

    reputable indian pharmacies https://indiaph24.store/# india pharmacy mail order
    pharmacy website india

  8. 7 Things About Double Glazed Window Repairs Near Me You’ll Kick
    Yourself For Not Knowing double glazed window repair

  9. bravo Case表示:

    7 Tips To Make The Most Out Of Your CSGO Case New bravo Case

  10. How To Determine If You’re In The Right Place For Double Glazing Windows Leeds leeds double glazing repairs

  11. Pedroereva表示:

    mexico drug stores pharmacies: mexico pharmacy – mexican mail order pharmacies

  12. JustinCit表示:

    buy prescription drugs from india https://indiaph24.store/# indian pharmacy online
    online shopping pharmacy india

  13. Michaelcew表示:

    http://mexicoph24.life/# medication from mexico pharmacy

  14. home jobs表示:

    17 Reasons You Shouldn’t Ignore Work Jobs From Home home jobs

  15. Rickysem表示:

    п»їbest mexican online pharmacies buying from online mexican pharmacy mexican rx online

  16. 15 Surprising Stats About Double Glazing Window Repairs Near Me
    double glazing window repair

  17. The 12 Most Popular Double Glazing Repairs Leeds Accounts To Follow On Twitter Window doctor leeds

  18. JustinCit表示:

    online shopping pharmacy india https://indiaph24.store/# indian pharmacy online
    indian pharmacy paypal

  19. Aw, this was a very good post. Spending some time and actual effort to create a
    great article… but what can I say… I hesitate a lot
    and don’t manage to get nearly anything done.

  20. 9 . What Your Parents Taught You About Upvc Windows Near Me upvc windows near me

  21. cheaper表示:

    Who Is Responsible For The Tommy Hilfiger Xbody Bag Budget?
    12 Ways To Spend Your Money cheaper

  22. Designer Handbags To Sell Tips From The Top In The Industry Designer Handbags Sale Outlet Uk

  23. Five Killer Quora Answers On Under Desk Treadmill Uk under desk treadmill uk

  24. Handles表示:

    20 Quotes That Will Help You Understand Double Glazed Units Manufacturers Near Me Handles

  25. How To Know If You’re Ready For Mens Masturbation Toys Men Masturbator

  26. lihat disini表示:

    Hi! Ɗo you know if they make any pluցіns to
    protect against hackerѕ? I’m kinda paranoid about losing everything I’ve worked hɑrd on.
    Any recommendations?

    my site; lihat disini

  27. What’s up everybody, here every one is sharing these experience,
    thus it’s pleasant to read this weblog, and I used to visit this weblog daily.

  28. vo_ntorg_vzot表示:

    Воєнторг
    18. Обмундирование и снаряжение для полевых условий
    металошукач металошукач .

  29. CUROZ表示:

    I’m impressed, I have to admit. Seldom do I come across a blog that’s equally educative and entertaining, and
    without a doubt, you’ve hit the nail on the head.
    The issue is something which not enough people are speaking intelligently about.
    I’m very happy that I stumbled across this in my search for something relating to this.

發佈留言

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