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,447,339 Responses

  1. Carrolleruck表示:

    https://canadianmdpharm.shop/# canadian pharmacy oxycodone
    maple leaf pharmacy in canada

  2. Brandonnot表示:

    Mexican Easy Pharm: reputable mexican pharmacies online – Mexican Easy Pharm

  3. CurtisBam表示:

    IndianCertPharm: Indian pharmacy that ships to usa – Indian pharmacy international shipping

  4. kypit 1s_vaEn表示:

    программа 1с купить программа 1с купить .

  5. CurtisBam表示:

    canadian pharmacy online reviews: CanadianMdPharm – canadian pharmacy prices

  6. CurtisBam表示:

    medication from mexico pharmacy: buying from online mexican pharmacy – mexico drug stores pharmacies

  7. After going over a handful of the articles on your site, I really
    appreciate your technique of writing a blog. I bookmarked it
    to my bookmark website list and will be checking back in the
    near future. Please visit my website too and let me know
    what you think.

  8. CurtisBam表示:

    canada pharmacy online: CanadianMdPharm – onlinecanadianpharmacy 24

  9. kypit 1s_wior表示:

    купить программу 1с бухгалтерия купить программу 1с бухгалтерия .

  10. kypit 1s_wror表示:

    купить 1с бухгалтерия купить купить 1с бухгалтерия купить .

  11. kypit 1s_cfor表示:

    купить 1с бухгалтерия 8.3 проф версия цена купить 1с бухгалтерия 8.3 проф версия цена .

  12. sugarmd表示:

    Your method of explaining all in this post is truly pleasant,
    all be capable of without difficulty know it, Thanks a lot.

  13. kypit 1s_zqEn表示:

    1с предприятие купить программу http://www.svarog.forum24.ru/?1-0-0-00000330-000-0-0 .

  14. The Ultimate Guide To Software For SEO Ranking Seo Tool

  15. AndreDib表示:

    legitimate canadian online pharmacies https://canadianmdpharm.com/# canadian pharmacy meds review
    Online medicine home delivery

  16. Welcome bonuses at Bitcoin casinos can come in many forms,
    including free spins, deposit bonuses, or a combination of both.

  17. CurtisBam表示:

    Best Indian pharmacy: Indian Cert Pharm – Indian Cert Pharm

  18. RileyBok表示:

    Viagra * Cialis * Levitra

    All the products you are looking an eye to are currently close by for the duration of 1+1.

    4 more tablets of one of the following services: Viagra * Cialis * Levitra

    https://xn--2i0bm4p0sf2wh7vdmsy.net

  19. AngelCroft表示:

    http://www.freshlyme.ru — Всё для вашего идеального кухонного пространства.

  20. kypit 1s_paEn表示:

    купить программу 1с бухгалтерия купить программу 1с бухгалтерия .

  21. kypit 1s_ykEn表示:

    1с предприятие купить программу 1с предприятие купить программу .

  22. Excellent website. Plenty of useful info here.
    I’m sending it to some pals ans additionally sharing in delicious.
    And certainly, thanks on your effort!

  23. AngelCroft表示:

    http://www.freshlyme.ru — Доступный сервис для проектирования кухни.

  24. AndreDib表示:

    canadian pharmacy 24h com https://mexicaneasypharm.com/# Mexican Easy Pharm
    Online medicine order

  25. kypit 1s_kaEn表示:

    программа 1с купить с установкой программа 1с купить с установкой .

  26. AndreDib表示:

    canadian pharmacy india https://canadianmdpharm.online/# canadian drugs pharmacy
    buy prescription drugs from india

  27. Brandonnot表示:

    Indian pharmacy international shipping: india pharmacy – Indian Cert Pharm

  28. AndreDib表示:

    safe online pharmacies in canada https://canadianmdpharm.com/# canada discount pharmacy
    reputable indian online pharmacy

發佈留言

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