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,236,998 Responses

  1. Тут можно преобрести шкафы для оружия сейфы сейф под оружие цена

  2. Тут можно преобрести сейфы для оружия купить оружейный сейф на заказ москва

  3. Тут можно преобрести оружейные сейфы шкафы сейф для хранения оружия

  4. ads network表示:

    There’s certainly a lot to find out about this subject.
    I like all the points you’ve made.

    Feel free to visit my blog – ads network

  5. Тут можно преобрести оружейные шкафы и сейфы оружейные шкафы в москве

  6. Diplomi_dper表示:

    купить диплом с регистрацией купить диплом с регистрацией .

  7. Тут можно преобрести купить сейф оружейный в москве сейф оружейный

  8. Mystery box表示:

    Guide To Mystery Boxes: The Intermediate Guide For Mystery Boxes Mystery box

  9. LarryCoell表示:

    ultrabet tr online ultrabet guncel ultrabet

  10. This page truly has all of the information and facts I wanted concerning this subject and didn’t know who to
    ask.

  11. 7 Easy Tips For Totally Rolling With Your
    Lightweight Foldable Wheelchairs Lightweight Folding Wheelchair In A Bag

  12. Тут можно преобрести оружейные сейфы интернет магазин оружейные сейфы в москве

  13. A Provocative Remark About Renault Clio Key Replacement renault clio Key replacement Cost

  14. Could Upvc Door Repair Near Me Be The Key To Achieving 2024?
    emergency upvc door repair

  15. tubidy表示:

    Great blog you have here but I was curious about if you
    knew of any discussion boards that cover the same topics
    talked about in this article? I’d really like to
    be a part of online community where I can get feedback from other knowledgeable people that share the same
    interest. If you have any recommendations, please let me know.
    Thanks!

  16. LarryCoell表示:

    Canl? Casino Siteleri casino siteleri win casino siteleri win

  17. Guide To Couches L Shape: The Intermediate Guide For Couches L Shape Couches l shape

  18. madepics.net表示:

    Quality articles or reviews is the crucial to
    invite the users to pay a quick visit the site, that’s what this web page is providing.

  19. prozone表示:

    Hello to every body, it’s my first payy a visit off this blog; this web site consists of awesome and actually fine
    stuff designed forr visitors.

    Feel free to surf to my page: prozone

  20. Diplomi_qler表示:

    купить ксиву prema365-diploms.ru .

  21. advertising表示:

    Hello, just wanted to say, I enjoyed this article. It was helpful.

    Keep on posting!

    Feel free to surf to my web page :: advertising

  22. Spot on with this write-up, I honestly feel this site needs a
    great deal more attention. I’ll probably be back again to see more, thanks for the information!

  23. Having read this I believed it was rather informative.
    I appreciate you taking the time and effort to put this information together.

    I once again find myself personally spending a lot of time both reading and commenting.
    But so what, it was still worth it!

    My blog post – rex parker crossword answers official

  24. rarbg表示:

    If you desire to improve your know-how simply keep visiting this website and be updated with the hottest news update posted here.

  25. Drug information sheet. Cautions.
    where to buy cheap verapamil price
    Everything news about drug. Read information now.

  26. Diplomi_vner表示:

    купить диплом учителя начальных классов prema365-diploms.ru .

  27. Hershel表示:

    What’s The Job Market For Fabric Two Seater Sofa Professionals Like?
    fabric two seater sofa [Hershel]

發佈留言

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