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,362,981 Responses

  1. RamonRek表示:

    ozempic: ozempic – ozempic

  2. Frankevild表示:

    http://ozempic.art/# buy cheap ozempic

  3. continuously i used to read smaller articles which also clear their
    motive, and that is also happening with this article
    which I am reading now.

  4. нарколог на дом вывод из запоя ростов нарколог на дом вывод из запоя ростов .

  5. BarryDof表示:

    https://rybelsus.shop/# rybelsus pill

  6. The Top Reasons Why People Succeed On The Coffee Pods Machine Industry Pod Coffee Machine

  7. Five Killer Quora Answers On Toto4d toto4d

  8. Amparo Guess表示:

    The Three Greatest Moments In Buy Tilt And Turn Windows History Buy Tilt And Turn Windows Online – Securityholes.Science

  9. Frankevild表示:

    https://rybelsus.shop/# rybelsus cost

  10. вывод из запоя в ростове на дону вывод из запоя в ростове на дону .

  11. 9 Things Your Parents Taught You About Titration ADHD Medications titration Adhd Medications

  12. What Is Foldable Wheelchairs For Sale And Why Is Everyone Speakin’ About
    It? Lightweight Wheelchair Foldable

  13. 15 Inspiring Facts About Keys Programmed That You Never Known Car Key Programming And Cutting

  14. What Is The Future Of Buy Realistic Sex Doll Be Like In 100 Years?
    buy realistic sex dolls

  15. Benito表示:

    Peculiar article, totally what I needed.

  16. В магазине сейфов предлагают купить сейф в москве цена где купить сейф

  17. Why You Should Concentrate On Improving Electric Fireplace Heater Freestanding retro electric fireplaces – Jay,

  18. В магазине сейфов предлагают модели сейфов сейфы купить

  19. Jeremypag表示:

    buy rybelsus online cheapest rybelsus pills buy semaglutide pills

  20. 10 Meetups About Freestanding Electric Range You Should Attend best freestanding electric fireplaces (Madeleine)

  21. Childrens Bunk Beds Tips To Relax Your Everyday Lifethe Only Childrens Bunk Beds Trick That Every Person Should Learn childrens bunk bed

  22. 5 Laws That Anyone Working In Audi Replacement Car Keys Should Know audi Replacement key cost

  23. Jeremypag表示:

    Ozempic without insurance ozempic cost ozempic coupon

  24. key Audi表示:

    The 9 Things Your Parents Taught You About Key Audi key Audi

  25. Frankevild表示:

    http://rybelsus.shop/# rybelsus pill

  26. Lucile Shank表示:

    24-Hours To Improve Sectional Sofa L Shaped L shaped sofa grey

  27. Link Building Insider Tips – A Six-Step Generating 1 Way Links Strategic Plan 토렌트모음 (https://heavenarticle.com/author/waveitaly4-943039/)

  28. 백링크表示:

    Top Seo Myths – Part 1 백링크

發佈留言

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