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,365,247 Responses

  1. 5 Killer Quora Answers On Double Glazed Window Repairs Near Me Double glazed Window repairs Near me

  2. 10 Things We Are Hateful About American Fridges Freezers narrow american fridge freezer

  3. Rvolchansk.ru表示:

    What’s The Reason Nobody Is Interested In L Couches For Sale sectional couch
    bed – Rvolchansk.ru,

  4. 14 Cartoons About Automotive Locksmiths That’ll
    Brighten Your Day automotive locksmiths near Me

  5. See What Replacement Sash Windows Tricks The Celebs Are Using replacement sash windows

  6. 11 “Faux Pas” That Are Actually OK To Make With Your Door
    Fitting Leeds window hinge repair leeds (https://bookmarkfeeds.Stream/)

  7. Lyle表示:

    Five People You Must Know In The Designer Handbags For Sale Industry designer handbags
    women’s (Lyle)

  8. Flor表示:

    This Week’s Best Stories About Double.Glazing Near Me double glazing near
    me (Flor)

  9. 14 Smart Strategies To Spend The Remaining Window Repairs Leeds Budget Upvc hinge repair leeds

  10. The Reason Everyone Is Talking About Volvo Digger Key Today
    Volvo S40 Key Fob Not Working

  11. What Is The Secret Life Of Best CSGO Case To Open Danger zone case

  12. کس کردن表示:

    Regards, I like this!

  13. 15 Reasons Not To Be Ignoring Michael Kors Bags Pink michael Kors bags Sling

  14. 15 Reasons To Not Ignore Double Glazed Near Me double glazing upvc windows

  15. Excellent blog here! Also your site loads up fast! What web host are
    you using? Can I get your affiliate link to your host?
    I wish my website loaded up as quickly as yours lol

  16. партнерствуем с ведущими нотариусами Москвы, что обеспечивает лучшие цены на заверение переводов.

    Have a look at my web page – https://public.tableau.com/app/profile/kursiv.ua/vizzes

  17. Bqwksi表示:

    order famciclovir generic – valcivir brand valaciclovir 1000mg canada

  18. window repair表示:

    5 Killer Quora Answers To Sash Window Repair window repair

  19. 5 Must-Know Practices For Double Glazing Door Repairs Near Me In 2023 double glazing Door

  20. 10 Things You’ve Learned In Kindergarden That’ll Help You With Free Spin Slots newest slots – shinhwaspodium.com

  21. 10 Quick Tips About Buy Mobility Scooters Near Me best electric scooter to buy

  22. You’ll Be Unable To Guess Double Glaze Repair Near Me’s Benefits
    double glaze repair near me

  23. 9 Things Your Parents Teach You About Railroad Injuries Lawsuit railroad Injuries Lawsuit

  24. JustinCit表示:

    online shopping pharmacy india https://indiaph24.store/# top online pharmacy india
    п»їlegitimate online pharmacies india

  25. Ten Things You Learned At Preschool To Help You Get A Handle On Car Locks Smith Locksmith for car

  26. sell表示:

    5 Must-Know-Practices Of Sell Perfume From Home For 2023
    sell

  27. 5 Clarifications On Mobile Car Locksmith Local Locksmiths For Cars

  28. A Guide To Double Glazing Leeds From Beginning To End upvc door repairs roundhay

發佈留言

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