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,234,961 Responses

  1. Davidtes表示:

    prinivil 10 mg prinivil cost lisinopril 2.5 mg medicine

  2. Davidphorn表示:

    Stumbled upon interesting material – I can’t help but recommend you to read https://millioner.5bb.ru/viewtopic.php?id=7376#p22334

  3. injury lawyer表示:

    9 Things Your Parents Teach You About Injury Lawyer injury lawyer

  4. Why We Do We Love Mesothelioma Lawsuits (And You Should, Too!) Asbestos Compensation

  5. The Most Important Reasons That People Succeed In The Dangerous Drugs Lawyers Industry Dangerous Drugs Attorneys

  6. near表示:

    10 Websites To Help You Learn To Be An Expert In Double Glazed Window
    Repair Near Me near

  7. Davidtes表示:

    tamoxifen for gynecomastia reviews tamoxifen and weight loss tamoxifen joint pain

  8. Davidtes表示:

    buy cytotec online buy cytotec online fast delivery buy cytotec in usa

  9. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 12.5

  10. Really plenty of fantastic information.

  11. Dirk表示:

    20 Insightful Quotes About Selling Avon Online Only direct selling avon (Dirk)

  12. WilliamRhype表示:

    http://finasteride.store/# get generic propecia without dr prescription

  13. 10 Things You’ve Learned About Preschool That’ll Help You Understand Online Shopping Uk Groceries
    Husaberg oil filter

  14. WilliamRhype表示:

    http://cytotec.club/# buy misoprostol over the counter

  15. Davidtes表示:

    where to get nolvadex tamoxifen generic tamoxifen moa

  16. How To Beat Your Boss On Double Glazing Showrooms Near
    Me Double Glazing Door Lock (https://Qooh.Me)

  17. WilliamRhype表示:

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

  18. Davidtes表示:

    nolvadex 10mg nolvadex pills nolvadex for pct

  19. Roseann表示:

    10 Places That You Can Find Double Glazed Units Near Me
    wood double glazing windows – Roseann

  20. It Is A Fact That What CSGO Cases Have Butterfly Knives Is The
    Best Thing You Can Get. What CSGO Cases Have Butterfly Knives Spectrum 2 Case

  21. Akcyza usa表示:

    Hello There. I found your blog using msn. This is a really well written article.

    I will make sure to bookmark it and come back to read more of your useful information. Thanks for the post.
    I’ll certainly return.

  22. Ronaldpsymn表示:

    http://ciprofloxacin.tech/# buy ciprofloxacin

  23. Accident表示:

    Boat Accident Case Tools To Ease Your Daily Lifethe One Boat Accident Case Technique Every Person Needs To Learn Accident

  24. Davidtes表示:

    cheap propecia order cheap propecia prices buying generic propecia without prescription

  25. RandallTaulp表示:

    ciprofloxacin: ciprofloxacin 500 mg tablet price – ciprofloxacin mail online

  26. An outstanding share! I’ve just forwarded this onto a colleague who has been conducting a
    little research on this. And he actually bought me dinner simply because I stumbled upon it for him…
    lol. So let me reword this…. Thanks for the meal!!
    But yeah, thanks for spending some time to discuss this topic here on your website.

  27. Oh my goodness! Amazing article dude! Many thanks, However I am experiencing issues with your RSS.
    I don’t understand why I cannot join it. Is there anyone else getting the same RSS issues?

    Anyone that knows the answer can you kindly respond?
    Thanx!!

  28. Ronaldpsymn表示:

    https://cytotec.club/# purchase cytotec

發佈留言

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