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,265,642 Responses

  1. Phillip May表示:

    I found this very helpful. For additional info, visit personal injury lawyer mobile al

  2. Amazing! Its actually amazing article, I have got much clear idea about from
    this article.

  3. Jamesdiure表示:

    пинап казино: пин ап казино онлайн – пин ап казино

  4. CurtisIncix表示:

    pinup kazi: пин ап казино – пин ап вход

  5. вывод из запоя в стационаре воронежа aqvakr.forum24.ru/?1-7-0-00011578-000-0-0-1730749158 .

  6. лечение наркозависимости в стационаре automobilist.forum24.ru/?1-19-0-00000140-000-0-0-1730749275 .

  7. Do you have any video of that? I’d care to find out more details.

  8. Five Killer Quora Answers To Repair Key Fob repair key fob

  9. Josephmax表示:

    https://vavada-kazi.ru/# вавада казино

  10. DarioTob表示:

    пин ап вход: pinup – pinup-kazi.ru

  11. Hey would you mind sharing which blog platform you’re
    using? I’m planning to start my own blog soon but I’m having a difficult time deciding
    between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique.
    P.S Apologies for being off-topic but I had to ask!

  12. CurtisIncix表示:

    пин ап зеркало: пин ап вход – пинап казино

  13. Drugs information for patients. Effects of Drug Abuse.
    order generic thorazine for sale
    Some information about medicine. Read information now.

  14. Asking questions are in fact pleasant thing if you
    are not understanding something fully, but this article gives pleasant understanding
    even.

  15. DarioTob表示:

    казино вавада: vavada – вавада казино

  16. HenrydeT表示:

    pinup пин ап казино пин ап казино

  17. Jesse Olson表示:

    Any person checking out styles like wide-plank lumber floorings? They are actually undoubtedly creating waves this period! # # anyKey word # carpet near me

  18. вывод из запоя в стационаре воронежа http://zavitai.mybb.social/viewtopic.php?id=91 .

  19. Truly a good deal of great knowledge!

    Feel free to surf to my web site – https://creditfreethai.com/casino-online/

  20. DarioTob表示:

    pinup-kazi.kz: pin up казино – пин ап кз

  21. LsgPaish表示:

    Exceptional prices allow you to tamoxifen and fatigue only after you have looked at competitive online specials

  22. This Site表示:

    Paragraph writing is also a excitement, if you be familiar with after that you can write if not it is complicated to write.

  23. выведение из запоя воронеж стационар https://www.setter.borda.ru/?1-7-0-00000675-000-0-0-1730749534 .

  24. I every time used to read paragraph in news
    papers but now as I am a user of internet therefore from now I am using net
    for posts, thanks to web.

發佈留言

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