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,233,827 Responses

  1. Pedroereva表示:

    buying prescription drugs in mexico: cheapest mexico drugs – mexico drug stores pharmacies

  2. Michaelcew表示:

    http://mexicoph24.life/# mexican border pharmacies shipping to usa

  3. It’s Time To Upgrade Your CS GO Weapon Case Options
    Snakebite Case

  4. What Mesothelioma Law Is Your Next Big Obsession mesothelioma settlement

  5. Rickysem表示:

    canadian pharmacy 365 canadian pharmacy sarasota best canadian online pharmacy

  6. Marcelabelp表示:

    http://indiaph24.store/# buy prescription drugs from india

  7. Here you can read and see the worlds lastest news
    and trend.

  8. トルソー 型 ラブドール ダッチワイフはどこで購入できますか?真の友人監査–TrueCompanion.comレビューあなたの古いダッチワイフを配置するための最良のアプローチそばかすのあるトップ7の最も合理的なダッチワイフ

  9. The Reason Why Evening Work From Home Jobs Is Everyone’s Desire In 2023 Working remotely from Home

  10. Shad表示:

    The Ugly Truth About Best CSGO Opening Site counter-strike cases (Shad)

  11. Gabriel表示:

    The 9 Things Your Parents Teach You About Car Key
    Fob Replacement car key fob replacement (Gabriel)

  12. Michaelcew表示:

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

  13. Rickysem表示:

    reputable mexican pharmacies online mexico pharmacy reputable mexican pharmacies online

  14. 11 Ways To Destroy Your Adhd In Women Symptoms Adhd in adults symptoms uk

  15. Marcelabelp表示:

    https://indiaph24.store/# Online medicine home delivery

  16. Slot Pulsa表示:

    Pⅼease let me knoѡ iff you’re looking fоr a article author fοr ʏoᥙr site.
    Yоu hаvе soke really great posts and I think Ι would be a
    ɡood asset. If уou ever want to takе some of thе load ⲟff, I’d absoluteⅼy love to ѡrite ѕome c᧐ntent foor уoսr
    bkog in exchange forr а lknk Ьack to mіne. Pⅼease send me an e-mail iff іnterested.
    Many tһanks!

    Ηere is mʏ webpage Slot Pulsa

  17. Shonda表示:

    What’s The Job Market For Walking Machine For Desk Professionals?
    walking machine for desk [Shonda]

  18. How Mesothelioma Law Was The Most Talked About Trend In 2023 mesothelioma Legal

  19. 15 Reasons Why You Shouldn’t Ignore Replacement Windows Leeds Window Replacement Leeds

  20. jamuslot表示:

    Very rapidly this wеb site ᴡill be fwmօus among ɑall blogging viewers, duue to it’ѕ nice
    articleѕ

    Ηere is mmy homepage – jamuslot

  21. Pedroereva表示:

    mexican online pharmacies prescription drugs: Mexican Pharmacy Online – mexican mail order pharmacies

  22. 10 Automobile Locksmith Tricks Experts Recommend locksmith Automotive

  23. Lawyers表示:

    Veterans Disability Lawyers Tools To Help You Manage Your Daily Lifethe One Veterans Disability Lawyers Trick That
    Everybody Should Learn Lawyers

  24. Think You’re Perfect For Leeds Double Glazing?
    Check This Quiz Upvc Window repairs leeds

  25. Audi Key Explained In Fewer Than 140 Characters audi Replacement keys

  26. 25 Amazing Facts About Combo Washing And Dryer Combo Washing And Dryer

  27. Marcelabelp表示:

    http://indiaph24.store/# top online pharmacy india

發佈留言

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