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,232,675 Responses

  1. Coffee Machine Explained In Fewer Than 140 Characters Barista machines

  2. 15 Startling Facts About Defra Wood Burning Stoves UK You’ve Never Known DEFRA stoves near me (morphomics.science)

  3. We are a group of volunteers and starting a
    new scheme in our community. Your website provided us with valuable info
    to work on. You’ve done an impressive job and our entire community will be thankful to you.

  4. It’s Time To Expand Your Mesothelioma Settlement Options Marion mesothelioma Lawyer

  5. Attractive section of content. I just stumbled upon your weblog and
    in accession capital to assert that I get actually enjoyed account your blog posts.
    Anyway I’ll be subscribing to your augment and even I achievement you access
    consistently quickly.

  6. 20 Resources That Will Make You Better At Shopping Online Uk Clothes Online Shopping

  7. What’s The Job Market For Private Psychiatrist Newcastle Professionals?
    Private psychiatrist newcastle

  8. Breanna表示:

    Five Killer Quora Answers To Double Glazing Repairs Leeds double glazing repairs leeds (Breanna)

  9. Jaerca表示:

    buy nitroglycerin without a prescription – nitroglycerin ca diovan 80mg canada

  10. Рекомендую надежную Букмекерскую контору 1win

  11. GeorgeSep表示:

    super kamagra: kamagra oral jelly – buy Kamagra

  12. 20 Things That Only The Most Devoted New Slots Online Fans Understand
    Slots with bonuses

  13. ElmerWes表示:

    Levitra tablet price levitrav.store Buy generic Levitra online

  14. Online Shopping Website In London Techniques To Simplify Your Everyday Lifethe Only
    Online Shopping Website In London Trick That Every Person Must Be Able To online shopping Website in london

  15. Phillipgam表示:

    Заказать SEO продвижение сайтов https://seoshnikigo.ru/ в ТОП поисковых систем Яндекс и Google в Москве, оплата за результат и по факту. Кейсы, стратегии продвижения, скидки и акции, индивидуальный подход

  16. CharlesDog表示:

    http://levitrav.store/# Cheap Levitra online

  17. TylerPhona表示:

    https://cialist.pro/# Buy Cialis online

  18. key表示:

    What’s The Job Market For Mercedes Replacement Key Cost Uk Professionals Like?
    key

  19. Five Things You Don’t Know About Ford Car Key Ford Transit Keys

  20. CharlesDog表示:

    https://cialist.pro/# Generic Cialis price

  21. CharlesDog表示:

    http://cialist.pro/# Buy Cialis online

  22. 10 Things People Hate About Replacement Volvo Keys spare Volvo key

  23. 5 Qualities People Are Looking For In Every Bentley Bentayga
    Key bentley continental Gt Key

  24. JamesNow表示:

    Kamagra 100mg price: kamagra.win – Kamagra Oral Jelly

  25. kilowatt Case表示:

    This Week’s Top Stories About How To Get CS GO Cases How To Get CS GO Cases kilowatt Case

  26. How Do I Explain Marc Jacobs Bag – Black To
    A 5-Year-Old marc jacobs bag Shoulder

  27. Shari表示:

    11 Ways To Completely Sabotage Your List Of Online
    Shopping Sites In Uk Office Artificial Plant Decor (Shari)

  28. ElmerWes表示:

    Viagra without a doctor prescription Canada Cheapest place to buy Viagra Viagra Tablet price

  29. TylerPhona表示:

    http://viagras.online/# generic sildenafil

  30. DavidOwelo表示:

    https://levitrav.store/# Levitra online pharmacy

發佈留言

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