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,158,395 Responses

  1. конференц залы оснащение конференц залы оснащение .

  2. Can you tell us more about this? I’d love to find out more details.

  3. конференц залы оснащение конференц залы оснащение .

  4. оборудование для конференц зала оборудование для конференц зала .

  5. How To Explain Barista Espresso Machine To A Five-Year-Old Mini espresso Machine

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

  7. конференц залы оснащение конференц залы оснащение .

  8. A Step-by Step Guide For Choosing Your Upvc External Doors Upvc Replacement Door Handles

  9. SEO_yiMa表示:

    продвижение сайтов в москве недорого https://prodvizhenie-sajtov-v-moskve115.ru/ .

  10. Hey There. I found your blog using msn. This is an extremely well written article.
    I will make sure to bookmark it and come back to read more of your useful information. Thanks for the
    post. I’ll certainly return.

  11. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://minimasvalls.es/yzbwsn of type and scrambled it to make a type specimen book.

  12. 15 Unquestionably Good Reasons To Be Loving Semi Truck Attorney Semi Truck Accidents

  13. lynnbolvin表示:

    10 Things We Love About Electric Wall Mounted Fireplace lynnbolvin

  14. I’ve been exploring Candy AI for a few weeks now and consider it quite impressive. Nonetheless, I initially found the pricing structure a bit confusing. To clarify, Candy AI offers a dual subscription model—both free and paid.

  15. Spot on with this write-up, I really believe this site needs a lot more attention. I’ll probably be returning to read
    through more, thanks for the information!

  16. This is very interesting, You are a very skilled blogger.
    I’ve joined your rss feed and look forward to seeking more of your great post.
    Also, I’ve shared your website in my social networks!

  17. Megaindex.ru表示:

    How Do I Explain Semi Truck Lawsuits To A Five-Year-Old semi truck accident lawsuit, Megaindex.ru,

  18. звуковое оборудование для конференц зала звуковое оборудование для конференц зала .

  19. Hi there every one, here every one is sharing these experience,
    so it’s good to read this webpage, and I used to go to see this web site daily.

  20. Espresso Coffee Machine Strategies That Will Change Your Life Espresso maker barista

  21. Trim Keto Boost is a popular dietary supplement that has gained a reputation for its ability to help people lose weight and improve their overall health.

    Feel free to browse page: https://jejusw.kr/index.php?mid=board_WevD37&document_srl=381219

  22. Medicament prescribing information. Brand names.
    how to buy generic ramipril price
    Some what you want to know about medicines. Get here.

  23. Five Killer Quora Answers To Best 18 Wheeler Accident Attorneys 18 Wheeler Accident Attorneys

  24. No matter if some one searches for his vital thing, thus he/she wishes to
    be available that in detail, thus that thing is maintained over here.

  25. The 10 Most Scariest Things About Online Store Uk Cheapest online store uk Cheapest

發佈留言

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