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,231,850 Responses

  1. Replacement Audi Key 10 Things I Wish I’d Known Sooner audi replacement key Fob

  2. JamesNow表示:

    Kamagra Oral Jelly: kamagra – cheap kamagra

  3. ElmerWes表示:

    Buy Cialis online Generic Cialis without a doctor prescription Cialis without a doctor prescription

  4. How Amazon Online Grocery Shopping Uk Was The Most Talked About Trend In 2023 Offroad Lighting Solutions

  5. Katrice表示:

    The Three Greatest Moments In Which CS GO Case Is
    Best To Open History shadow case (Katrice)

  6. Why All The Fuss? Mesothelioma Attorney? mesothelioma legal

  7. JamesNow表示:

    Cenforce 150 mg online: Buy Cenforce 100mg Online – cheapest cenforce

  8. Vimeo.Com表示:

    10 Healthy Semi Truck Settlement Habits Vimeo.Com

  9. 15 Terms Everybody In The Case Opening Sites CSGO Industry Should Know open cs2 Cases

  10. ElmerWes表示:

    buy Levitra over the counter Buy generic Levitra online Buy Vardenafil online

  11. See What Multi Fuel Stoves For Sale Near Me Tricks The Celebs Are Utilizing multi fuel stoves for sale

  12. JamesNow表示:

    Tadalafil price: buy cialis online – Buy Tadalafil 20mg

  13. JamesNow表示:

    buy Viagra online: Buy Viagra online cheap – over the counter sildenafil

  14. Near Me表示:

    What’s Holding Back From The Adhd Treatment Without Medication Industry?
    Near Me

  15. Solutions To The Problems Of Double Glazing Repair Near Me double Glazing near me

  16. CharlesDog表示:

    http://levitrav.store/# Levitra 10 mg buy online

  17. 12 Stats About Erb’s Palsy Attorney To Make You Think About The Other
    People howard Erb’s Palsy Lawyer

  18. JamesNow表示:

    cenforce.pro: cheapest cenforce – Buy Cenforce 100mg Online

  19. Why You’ll Want To Learn More About Renault Clio Key renault clio key fob, http://www.annunciogratis.net,

  20. GeorgeSep表示:

    cheap kamagra: kamagra – Kamagra tablets

  21. Fredrick表示:

    A Productive Rant About CSGO Weapon Case esports 2014
    summer case (Fredrick)

  22. ElmerWes表示:

    Generic Tadalafil 20mg price Cialis 20mg price in USA Buy Tadalafil 20mg

  23. ElmerWes表示:

    Buy Tadalafil 10mg п»їcialis generic Generic Cialis price

  24. Upvc Repair表示:

    30 Inspirational Quotes On Upvc Windows Repair Upvc Repair

  25. JamesNow表示:

    Cialis over the counter: cialist.pro – buy cialis pill

  26. What Is Asbestos Compensation? To Utilize It Asbestos Litigation

  27. 10 Things Everyone Hates About Replace Window Handle Replacement window Handle

  28. I waas able tto find good information from your content.

    Check out my blog post … Praca We WłOszech Dla Par

  29. JamesNow表示:

    Levitra 10 mg best price: Cheap Levitra online – Levitra price

  30. ElmerWes表示:

    Cheapest Sildenafil online Cheap Viagra 100mg cheap viagra

發佈留言

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