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,367,727 Responses

  1. naga169表示:

    It’s very trouble-free to find out any matter on net as compared
    to books, as I found this piece of writing at this website.

  2. Designer Handbags Beige Tools To Ease Your Daily Lifethe One Designer Handbags Beige Trick That Everybody Should Be Able To
    designer handbags beige

  3. Ada表示:

    5 Cliches About Folding Mobility Scooters Uk You Should Avoid scooters that fold up (Ada)

  4. Marcy表示:

    10 Top Mobile Apps For Bunk Bed For Adults buy bunk bed online [Marcy]

  5. Присоединяйтесь к группам объявлений России!
    Частные объявления всегда были и будут бесплатными.
    Не нарушайте правила групп, чтобы размещать Ваши объявления
    Присоединяйся на группу своего города, чтобы размещать объявления.. https://t.me/addlist/2JY5f6-xD9Y4ODIy

  6. Pedroereva表示:

    mexican drugstore online: Mexican Pharmacy Online – mexican drugstore online

  7. Marcelabelp表示:

    https://indiaph24.store/# pharmacy website india

  8. Freezer Table Top: What’s No One Has Discussed medical freezers
    (https://www.mazafakas.com)

  9. Slot Pulsa表示:

    Hello, for aⅼl tіme i սsed tօ check webpage posts herе early in thhe
    daylight, fօr the reason tһat i liқе to leaarn more and more.

    Alѕo visit my webpage … Slot Pulsa

  10. Why Personal Injury Settlement Still Matters In 2023 Personal injury lawsuits

  11. Pedroereva表示:

    medication from mexico pharmacy: cheapest mexico drugs – mexico drug stores pharmacies

  12. Pedroereva表示:

    medicine in mexico pharmacies: cheapest mexico drugs – mexican online pharmacies prescription drugs

  13. What’s The Current Job Market For Lost My Car Keys Professionals?
    Lost My Car Keys

  14. 15 Facts Your Boss Wishes You’d Known About
    Double Glazed Window Repair Near Me Double Glazed Front Door (Velo-Xachmas.Com)

  15. A Step-By Step Guide For Choosing The Right Repair Misted Double
    Glazing Near Me replacement double Glazed glass Only prices

  16. Rickysem表示:

    top 10 pharmacies in india indian pharmacy cheapest online pharmacy india

  17. The 10 Scariest Things About Prada Bag prada bags black (extension.unimagdalena.edu.co)

  18. Marcelabelp表示:

    https://canadaph24.pro/# recommended canadian pharmacies

  19. Michaelcew表示:

    http://indiaph24.store/# indian pharmacy

  20. Rickysem表示:

    prescription drugs canada buy online canada drugs online review best canadian pharmacy to buy from

  21. 20 Important Questions To Ask About Fleshlight Before You Buy Fleshlight buy Fleshlight

  22. Excellent post. I was checking continuously this blog and
    I’m impressed! Extremely helpful information specially the last part :
    ) I care for such information a lot. I was seeking this particular info for a
    long time. Thank you and best of luck.

  23. The Next Big New Used Sofas For Sale Industry sectional Couch with storage

  24. 10 Buying A Mobility Scooter-Related Projects That Stretch Your Creativity who buys mobility scooters

  25. Rickysem表示:

    canadian pharmacy 24h com Large Selection of Medications from Canada canadian pharmacy 1 internet online drugstore

  26. This History Behind Avon Glimmerstick Can Haunt You Forever!

    avon true colour Glimmerstick brow definer

  27. What’s The Job Market For Car Key Repair Shop Near Me Professionals?
    car key Repair Shop near Me

  28. Marcelabelp表示:

    https://mexicoph24.life/# medication from mexico pharmacy

  29. Rickysem表示:

    canadian family pharmacy canadian pharmacies the canadian drugstore

發佈留言

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