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,232,105 Responses

  1. WilliamRhype表示:

    http://cytotec.club/# cytotec abortion pill

  2. RandallTaulp表示:

    cheap lisinopril 40 mg: buy lisinopril without prescription – price of lisinopril 20 mg

  3. A How-To Guide For Mercedes Key From Beginning To
    End Replacement Mercedes key

  4. WilliamRhype表示:

    http://nolvadex.life/# nolvadex online

  5. Who Is Responsible For An Couches For Sale Budget?
    12 Top Ways To Spend Your Money Cheap Sofa bargains

  6. WilliamRhype表示:

    https://nolvadex.life/# how to get nolvadex

  7. Modesta表示:

    15 Gifts For The Treatment For Adult Adhd Lover In Your
    Life adhd treatment for adults uk (Modesta)

  8. 20 Resources To Make You More Efficient At Replacement Windows Leeds Upvc door repair meanwood

  9. Ronaldpsymn表示:

    http://finasteride.store/# buy propecia without dr prescription

  10. WilliamRhype表示:

    http://lisinopril.network/# online lisinopril

  11. skins表示:

    It’s The Complete Cheat Sheet For CS GO Gambling Sites skins

  12. 7 Little Changes That’ll Make The Biggest Difference In Your Double Glazed Repairs Near Me Double Glazing Units Near Me (https://Severinsen-Hickey-2.Thoughtlanes.Net/)

  13. You’ll Never Guess This Double Glazed Units Near Me’s Tricks Double Glazed Units Near Me

  14. RandallTaulp表示:

    lexapro and tamoxifen: nolvadex price – femara vs tamoxifen

  15. What’s The Current Job Market For Locksmith For A Car Professionals?
    locksmith for a car

  16. 20 Reasons To Believe Window Repair Near Will Never Be
    Forgotten Upvc Window Repair Near Me

  17. WilliamRhype表示:

    https://nolvadex.life/# tamoxifen postmenopausal

  18. RandallTaulp表示:

    buy propecia: cost of propecia pills – cost propecia prices

  19. 15 Gifts For The Mesothelioma Attorneys Lover In Your Life mesothelioma Compensation

  20. RED BOOST表示:

    I have read so many content on the topic of the blogger lovers but this paragraph is
    actually a pleasant post, keep it up.

  21. sex near ne表示:

    Here is my blog :: sex near ne

  22. Medical Malpractice Litigation Explained In Fewer Than 140 Characters medical malpractice Lawsuits

  23. 33poker表示:

    Check out my page … 33poker

  24. See What Auto Locksmiths Near Me Tricks The
    Celebs Are Using auto Locksmiths near me

  25. Nine Things That Your Parent Teach You About Window Handles Replacement
    Window handles replacement

  26. Don’t Believe In These “Trends” Concerning Locksmith For Car Near Me
    lost car keys Locksmith

  27. website,表示:

    Remarkable things here. I am very glad to see your article.
    Thanks so much and I’m having a look ahead to contact you.
    Will you please drop me a mail?

  28. You’ll Be Unable To Guess Best Robot Vacuum For Dog Hair’s Benefits robot Vacuum for dog Hair

  29. Ronaldpsymn表示:

    http://lisinopril.network/# how to order lisinopril online

  30. Davidtes表示:

    order generic propecia pills buy cheap propecia order generic propecia without rx

發佈留言

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