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

  1. Thomastat表示:

    Магазин спортивной фармакологии – OxyShop. Это всемирно известный надёжный поставщик качественных препаратов для спортсменов спортивная фарма форум

  2. У вас есть возможность обратиться к администрации через электронную почту.

  3. Marcelabelp表示:

    https://canadaph24.pro/# canada drugs online reviews

  4. Rickysem表示:

    canadian pharmacy online Certified Canadian Pharmacies canadian discount pharmacy

  5. Excellent article. I certainly appreciate this website. Continue the
    good work!

  6. Rickysem表示:

    best india pharmacy indian pharmacy indian pharmacies safe

  7. Rickysem表示:

    medication from mexico pharmacy mexican pharmacy mexican rx online

  8. JustinCit表示:

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

  9. The Reasons You Shouldn’t Think About Improving Your CSGO New Case
    Revolution case

  10. Marcelabelp表示:

    https://canadaph24.pro/# canadian drug stores

  11. rtp kasih4d表示:

    Hey, I think your website might be having browser compatibility
    issues. When I look at your blog in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, very
    good blog!

  12. Rickysem表示:

    Online medicine order Cheapest online pharmacy reputable indian online pharmacy

  13. Pedroereva表示:

    mexican drugstore online: mexico pharmacy – buying from online mexican pharmacy

  14. See What Vauxhall Key Fobs Tricks The Celebs Are Using Vauxhall key fob

  15. Rickysem表示:

    medication from mexico pharmacy buying prescription drugs in mexico medication from mexico pharmacy

  16. 10 Things You Learned From Kindergarden That Will Help You Get Uk Online Phone Shopping Sites High-Quality Painting Brush

  17. nds 117bc icv表示:

    This Is The Ugly Real Truth Of Online Shopping
    nds 117bc icv

  18. Marcelabelp表示:

    https://canadaph24.pro/# maple leaf pharmacy in canada

  19. 9 . What Your Parents Taught You About Auto Accident Lawsuit
    Auto Accident Lawsuit

  20. The Most Successful Places To Buy Electric Scooters Near Me Gurus
    Are Doing Three Things Places That Buy Mobility Scooters Near Me

  21. Клиент соглашается с правилами и условиями работы онлайн клуба Pokerdom,
    подтверждает свое совершеннолетие.

  22. bravo case表示:

    How To Know If You’re Prepared For CS GO Case New bravo case

  23. Marcelabelp表示:

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

  24. Pedroereva表示:

    purple pharmacy mexico price list: Mexican Pharmacy Online – mexican drugstore online

  25. JustinCit表示:

    Online medicine order http://indiaph24.store/# п»їlegitimate online pharmacies india
    world pharmacy india

  26. The No. Question That Everyone In Marc Jacobs Black Handbag Must Know
    How To Answer marc jacobs Bag tk Maxx

  27. Pedroereva表示:

    mexican online pharmacies prescription drugs: mexico pharmacy – mexico drug stores pharmacies

  28. Marcelabelp表示:

    https://canadaph24.pro/# canadianpharmacyworld

發佈留言

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