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,371,120 Responses

  1. Merry表示:

    Solar best energy plans australia (Merry) is clean. Gas plans are a lot simpler.

  2. The 10 Scariest Things About Wood Burner Stoves Uk wood burner stoves – Curt,

  3. Devon Holley表示:

    You’ll Never Guess This 60 40 Fridge Freezer Integrated’s Tricks 60 40 fridge Freezer integrated

  4. 20 Trailblazers Leading The Way In Best Kids Bunkbeds Kids Furniture

  5. Могу ли я сделать бронирование от имени другого https://www.odessamama.net/forum.php?mod=viewthread&tid=303338&extra= лица?
    Могу ли я работать с рестораном напрямую?

  6. BarryDof表示:

    https://rybelsus.shop/# rybelsus coupon

  7. Jeremypag表示:

    ozempic online ozempic generic ozempic coupon

  8. What is New U Life SOMADERM Transdermal
    Gel.

    Also visit my website … euphoria mushroom gummies

  9. What is New U Life SOMADERM Transdermal Gel.

    My web blog; functional mushroom gummies

  10. Hi there, just wanted to say, I enjoyed this post. It was funny.
    Keep on posting!

    My web blog – privately owned centers

  11. You’ll Never Guess This Sleeper Sectional With Chaise’s Tricks sleeper sectional With chaise

  12. They have items to sell or solutions to provide.

    Here is my web-site :: euphoria Mushroom Gummies

  13. Bookmarks表示:

    There are over 60 energy providers in Texas.

    Feel free to surf to my website Bookmarks

  14. Respect to article author, some fantastic information.

    Feel free to surf to my web-site :: child care service raleigh

  15. How To Get More Benefits Out Of Your Auto Folding Mobility Scooter Near Me auto Fold Mobility scooter uk, socialmphl.com,

  16. В магазине сейфов предлагают сейфы простые сейфы купить в москве

  17. Thanks for helping out, wonderful information.

    Check out my web site … daycare arlington va

  18. Bookmarks表示:

    The intelligence is the best completely dry herb vape I have actually attempted.

    Here is my homepage: Bookmarks

  19. Mobile vaporizers make use of batteries as a power source.

    Here is my page canadian plant based meat companies

  20. Jeremypag表示:

    ozempic ozempic ozempic

  21. Bookmarks表示:

    The vapor and flavor quality is likewise wonderful.

    Check out my homepage Bookmarks

  22. The average cost to move an electric meter of $675.

    Feel free to visit my homepage … energy efficient tax credit 2022 form

  23. Hi, just wanted to say, I liked this blog post. It was practical.

    Keep on posting!

    Feel free to surf to my web site; work from home

  24. Bookmarks表示:

    This vaporizer has a number of distinct features.

    My homepage; Bookmarks

  25. I leave a comment when I like starting a child care business
    post on a blog or if I have something to contribute to the discussion. Usually it’s a
    result of the passion displayed in the article I
    browsed. And on this article JS 依照元素出現數量排序 – 馬老師 雲端研究室.
    I was actually excited enough to drop a
    thought 😛 I do have a couple of questions for you if it’s
    allright. Could it be just me or does it look as if like some of the comments look
    like they are coming from brain dead individuals? 😛 And, if you
    are posting at additional sites, I’d like to keep up with everything fresh you have
    to post. Would you list the complete urls of all your social sites like your twitter feed, Facebook page
    or linkedin profile?

  26. You’ll Never Be Able To Figure Out This Upvc Door Doctor Near Me’s Tricks Door Doctor

  27. Bookmarks表示:

    Simply put, solar energy is energy from the sun.

    Here is my webpage Bookmarks

  28. Bookmarks表示:

    It’s time to look into energy comparison.

    Here is my web page Bookmarks

  29. We have actually aided 285 customers discover lawyers today.

    My web page; how to get a personal lawyer

發佈留言

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