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,466,948 Responses

  1. Wallacefex表示:

    denemebonusuverensiteler25: deneme bonusu veren siteler yeni – deneme bonusu veren yeni siteler

  2. Wallacefex表示:

    sweet bonanza: sweet bonanza – sweet bonanza

  3. 1x slots表示:

    Эта цитата подчеркивает уникальность и гибкость игрового опыта в PUBG, где игроки могут влиять на исход событий и создавать свои собственные игровые истории.

  4. BradleyEdino表示:

    sweet bonanza sweet bonanza kazanma saatleri sweet bonanza slot

  5. Wallacefex表示:

    deneme bonusu veren siteler: deneme bonusu veren yeni siteler – deneme bonusu veren yeni siteler

  6. Dealing With Pranksters With Reverse Phone Search 다바오포커

  7. whitecock表示:

    What’s Happening i’m new to this, I stumbled upon this I have
    discovered It positively helpful and it has aided me
    out loads. I am hoping to contribute & assist different customers
    like its aided me. Great job.

  8. Nancy Cole表示:

    Valuable information! Find more at sprinkler repair contractor

  9. BradleyEdino表示:

    yeni deneme bonusu veren siteler yat?r?ms?z deneme bonusu veren siteler yeni deneme bonusu veren siteler

  10. На сайтах об азартных играх еще не начали публиковаться рецензии,
    а пользователи не успели
    оставить отзывы и поделиться впечатлениями.

  11. BradleyEdino表示:

    sweet bonanza oyna sweet bonanza sweet bonanza demo oyna

  12. I know this if off topic but I’m looking into starting my
    own blog and was curious what all is required to get setup?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web smart so I’m not 100% sure. Any suggestions
    or advice would be greatly appreciated. Thank you

  13. Eleanore表示:

    Hello, after reading this amazing post i am as well delighted to
    share my know-how here with friends.

  14. Paul Tyler表示:

    This is highly informative. Check out Agence SEO for more

  15. I enjoyed this article. Check out عمل prk for more

  16. I admire how thorough and detailed your posts are.

  17. Gussie Patton表示:

    I observed your insights on emergency locksmith facilities truly powerful! Thanks for sharing! commercial locksmith

  18. Josie Roy表示:

    After seeing how stunning paver stones look in my neighbor’s yard, I’m convinced I need to redo mine! For inspiration and guidance, you should definitely see what’s available at low maintenance landscape

  19. BradleyEdino表示:

    yat?r?ms?z deneme bonusu veren siteler yeni deneme bonusu veren siteler deneme bonusu veren siteler yeni

  20. Thanks for finally writing about > JS 依照元素出現數量排序 – 馬老師 雲端研究室
    < Loved it!

  21. Justin Pope表示:

    Appreciate the useful tips. For more, visit bed bug exterminator

  22. Jennie表示:

    Celebrating A 40Th Birthday – Party Ideas 아이러브밤 (Jennie)

  23. Greate pieces. Keep posting such kind of information on Sell Your Motorcycle page.

    Im really impressed by it.
    Hi there, You have performed an incredible job. I’ll
    definitely digg it and personally suggest to my friends. I
    am sure they will be benefited from this website.

  24. The Advantages Of Playing Poker On-Line 에볼루션 영화

  25. BradleyEdino表示:

    deneme bonusu veren yeni siteler yeni deneme bonusu veren siteler deneme bonusu veren yeni siteler

  26. FrankEmine表示:

    denemebonusuverensiteler25: yeni deneme bonusu veren siteler – denemebonusuverensiteler25

發佈留言

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