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,363,359 Responses

  1. вывод из запоя ростов и область https://www.vyvod-iz-zapoya-rostov17.ru .

  2. Patio Door Lock Repair Near Me Tools To Help You Manage Your Everyday Lifethe Only Patio Door Lock Repair Near Me Technique Every Person Needs To Learn patio door lock repair (Elke)

  3. Jeremypag表示:

    ozempic coupon buy ozempic pills online ozempic generic

  4. Pretty nice post. I simply stumbled upon your weblog and wanted to mention that I’ve really enjoyed surfing around
    your blog posts. After all I’ll be subscribing to your feed and I hope you write
    once more soon!

  5. Darrellrit表示:

    Pill effects listed. Get medicine facts.
    clarithromycin cz
    Medication trends described. Find medication details.

  6. RamonRek表示:

    rybelsus pill: rybelsus price – cheapest rybelsus pills

  7. RamonRek表示:

    ozempic coupon: ozempic – buy ozempic pills online

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

  9. 강북호빠表示:

    강북호빠 이번에 새식구를 모집중에 있습니다.

  10. Guy Diederich表示:

    One Of The Most Innovative Things Happening With Locksmith Near Me For Car Local car Locksmiths

  11. Казино Cryptoboss – Официальный сайт для криптовалютных игр
    https://by-it.ru/

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

  13. What Is ADHD Treatment For Adults? History Of ADHD Treatment For Adults what Is adhd treatment for adults

  14. BarryDof表示:

    https://rybelsus.shop/# buy semaglutide online

  15. Frankevild表示:

    http://rybelsus.shop/# semaglutide online

  16. DichaelDax表示:

    Simply wish to say your article is as astonishing. The clarity on your publish is simply great and i can think you’re knowledgeable on this subject. Fine together with your permission let me to grab your RSS feed to stay updated with forthcoming post. Thanks a million and please keep up the enjoyable work.
    vovan casino

  17. BarryDof表示:

    https://rybelsus.shop/# rybelsus coupon

  18. Are You Tired Of Realistic Sex Doll For Sale? 10 Inspirational Ideas To Bring Back Your Love Realisticsex Dolls

  19. I like the valuable information you provide in your articles.
    I will bookmark your weblog and check again here regularly.
    I am quite certain I’ll learn plenty of new stuff right here!
    Best of luck for the next!

    Feel free to surf to my web page: emperor’s vigor tonic reviews

  20. BarryDof表示:

    https://ozempic.art/# ozempic generic

  21. 15 Replacement Windows Benefits Everyone Needs To Be Able To upvc window lock replacement

  22. torrent vpn表示:

    This information is worth everyone’s attention. How can I find out more?

    My website … torrent vpn

  23. Senaida Chin表示:

    The Story Behind Cut Car Keys Can Haunt You Forever!Broken Car Key

  24. Jeremypag表示:

    buy semaglutide online rybelsus pill buy semaglutide online

  25. Informative article, tottally what I wanted to
    find.

    My site قيمت ورق گالوانیزه

  26. Fredric Moran表示:

    See What Freestanding Electric Fireplace Heater Tricks The Celebs Are Using freestanding electric fireplace (Rosie)

  27. You’ll Never Guess This Mobility Scooter For Elderly’s Tricks Specialized Mobility Scooters

  28. Patti Walcott表示:

    10 Top Facebook Pages That I’ve Ever Seen. ADHD Private Assesment Private adhd assessment North east

  29. 20 Interesting Quotes About Ultra Lightweight
    Electric Wheelchair Mobility Electric chair (olderworkers.com.au)

發佈留言

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