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,234,229 Responses

  1. Are You Responsible For An Upvc Window Repairs Budget?

    10 Incredible Ways To Spend Your Money window repairs near Me

  2. Double Glazing Firms Near Me: 11 Things You’re Not Doing Double glazed Unit replacement

  3. Wood Burning Stove Small Tools To Make Your Daily Lifethe
    One Wood Burning Stove Small Trick Every Person Should Be Able
    To wood burning stove small

  4. Thanks for sharing your thoughts about проект домашнего кинотеатра.
    Regards

  5. 5 Killer Quora Answers On Auto Accident Attorneys auto Accident attorneys

  6. You’ll Never Guess This Upvc Door Repairs Near Me’s Tricks upvc door repairs Near Me

  7. Syair SGP表示:

    I like it whenever people come together and share views.
    Great website, stick with it!

  8. 10 Mesothelioma And Lawyers Projects Related To Mesothelioma And Lawyers To Extend Your Creativity Affordable Mesothelioma Attorney

  9. WilliamRhype表示:

    https://nolvadex.life/# pct nolvadex

  10. RandallTaulp表示:

    lisinopril tablets: lisinopril 5mg prices – lisinopril from mexico

  11. casino_cqKt表示:

    casino bono de bienvenida sin deposito peru https://sin-deposito-peru.org/ .

  12. It’s The Good And Bad About Designer Handbags Designer Handbags Pink [Bookmarkingworld.Review]

  13. RandallTaulp表示:

    does tamoxifen cause joint pain: tamoxifen cost – nolvadex 20mg

  14. You’ll Be Unable To Guess Nespresso Magimix Coffee Machine’s Tricks
    Nespresso Magimix Coffee Machine

  15. 14 Common Misconceptions Concerning Injury Law
    injury law firm

  16. Affordable SEO: The Good, The Bad, And The Ugly affordable seo Company Near me

  17. 9 . What Your Parents Taught You About Upvc Window Repairs upvc Window Repairs

  18. Hunter表示:

    A Look At The Ugly Facts About CS GO Case Website case spectrum (Hunter)

  19. casino_idKt表示:

    bono gratis para casino sin deposito peru https://sin-deposito-peru.org/ .

  20. replacement表示:

    Why You Should Focus On Enhancing Double Glazed Window Repair replacement

  21. Hi, Neat post. There’s a problem along with your web site in web explorer, would
    check this? IE still is the market chief and a big portion of other folks will miss
    your magnificent writing because of this problem.

  22. 10 Myths Your Boss Is Spreading About Birth Injury Attorneys birth injury Lawsuit

  23. You’ll Never Guess This Treadmill Sale UK’s Tricks Treadmill Sale Uk – Ilovebookmarking.Com,

  24. Do you mind if I quote a few of your posts as long as I provide credit and sources
    back to your site? My blog site is in the very same niche as yours and my visitors would truly benefit from a lot of the information you provide here.
    Please let me know if this okay with you.
    Many thanks!

  25. The Reasons ADHD Tests Is Harder Than You Imagine adult testing adhd

  26. What Is The Reason? Audi Battery Key Replacement
    Is Fast Becoming The Hot Trend For 2023 Replacement Audi Key Cost Uk

  27. It’s in point of fact a great and helpful piece of information. I’m satisfied that you shared this helpful
    info with us. Please keep us informed like this. Thank you for sharing.

  28. 20 Resources That Will Make You Better At Mesothelioma
    Law Asbestos legal

發佈留言

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