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,159,398 Responses

  1. Guide To Online Clothes Shopping Near Me: The Intermediate Guide Towards Online Clothes Shopping Near Me online clothes shopping Near me

  2. The Unspoken Secrets Of Personal Injury Case Personal Injury Attorney

  3. Pvc Window Repairs It’s Not As Expensive As You Think Upvc Window Repair

  4. Bookmarks表示:

    I’m the proprietor operator of GOPARTYBUS Perth.

    My web site: Bookmarks

  5. The Best Motor Vehicle Settlement Methods To Transform Your Life Motor vehicle accident Lawyer

  6. Why You Should Focus On Making Improvements To Private Adhd Assessment Adhd assessments

  7. Responsible For The Repairing Upvc Windows Budget?

    10 Amazing Ways To Spend Your Money upvc windows repair near Me

  8. Hi, I do think this is a great web site. I stumbledupon it 😉 I
    may revisit once again since i have saved as a favorite it.

    Money and freedom is the best way to change, may you be rich
    and continue to help other people.

  9. What is New U Life SOMADERM Transdermal Gel.

    My website; types of disability benefits

  10. Why Motor Vehicle Lawsuit Is Harder Than You Imagine Motor Vehicle Accident Lawsuit

  11. 5 Clarifications Regarding Birth Injury Lawyers Birth Injury Attorney

  12. pishing site表示:

    Superb website you have here but I was curious about if you knew of any message boards
    that cover the same topics talked about in this article? I’d really like to be a
    part of online community where I can get advice from other experienced people that share the same interest.
    If you have any recommendations, please let me know.

    Many thanks!

  13. What Is Birth Injury Lawyer And Why Is Everyone Speakin’ About
    It? birth injury Law firms

  14. 5 Killer Quora Answers On Shopping Online Uk Clothes shopping online uk
    clothes – teploenergodar.ru,

  15. Allow me show you in my evaluation of NewULife.

    my web blog; ai profile picture generator reddit

  16. оборудование для конференц связи https://oborudovanie-konferenc-zalov11.ru .

  17. Kent casino表示:

    Финансовые платежи, бонусы и верификация вынесены в отдельные блоки, вне личного кабинета пользователя.

  18. современный конференц зал http://oborudovanie-konferenc-zalov11.ru/ .

  19. оборудование для конференц зала готовые решения http://oborudovanie-konferenc-zalov11.ru/ .

  20. конференц зал оборудование оснащение конференц зал оборудование оснащение .

  21. 7 Simple Secrets To Totally Rocking Your Upvc Doors Repair replace Lock on upvc door

  22. 먹튀검증表示:

    Super Slots will double each and every transaction uup to
    $1,000, givinjg you a total of $six,000.

    Also visit my web blog; 먹튀검증

  23. Bookmarks表示:

    This brand uses organic hemp to make all its products.

    Here is my web page :: Bookmarks

  24. 17 Reasons Not To Not Ignore 18 Wheeler Truck Accident Attorney 18 wheeler Accident

  25. The Guide To Affordable SEO Expert In 2023 Affordable Seo Packages Usa

發佈留言

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