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,468,182 Responses

  1. Rosa Webb表示:

    Great tips! For more, visit best live casinos

  2. Edgar Obrien表示:

    This was quite informative. For more, visit Comment choisir un casino

  3. Micheline表示:

    Hi there, after reading this amazing post i am also cheerful to share my experience here with mates.

  4. Jonas表示:

    I used to be able to find good advice from your blog articles.

  5. Justin表示:

    A Look At The Myths And Facts Behind Buy A Driving License kup prawo jazdy b​ online [Justin]

  6. Shalanda表示:

    Great blog right here! Also your web site lots up fast!
    What web host are you the usage of? Can I get
    your associate hyperlink for your host? I desire my web site loaded up as fast
    as yours lol

  7. I really like your blog.. very nice colors & theme.
    Did you create this website yourself or did you hire
    someone to do it for you? Plz respond as I’m looking to construct my own blog and would like
    to know where u got this from. cheers

  8. Bart表示:

    What’s Happening i’m new to this, I stumbled upon this I have discovered It positively helpful and it has
    helped me out loads. I hope to give a contribution & aid different customers like its aided me.
    Great job.

  9. Tax Return Online – Makes Life Easy 다바오충전

  10. Sallie Watson表示:

    This was highly educational. For more, visit drain cleaning,

  11. 11 Strategies To Completely Redesign Your Replace
    Upvc Window Handle Door Handle Repair Near Me

  12. Does your site have a contact page? I’m having a tough time
    locating it but, I’d like to send you an e-mail.
    I’ve got some recommendations for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it develop over time.

  13. A Time-Travelling Journey: How People Talked About Fabric 2 Seater 20 Years Ago Fabric Sofa 2 Seater

  14. JasonUnsoG表示:

    https://casinositeleri25.com/# guvenilir casino siteleri

  15. Wallacefex表示:

    slot casino siteleri: az parayla cok kazandiran slot oyunlar? – az parayla cok kazandiran slot oyunlar?

  16. I know this web site gives quality depending posts
    and additional material, is there any other site which offers
    such data in quality?

  17. Archer C54表示:

    Hi I am so happy I found your web site, I really found you by error, while I was browsing on Google for something else, Anyways I am here now
    and would just like to say thanks a lot for a
    incredible post and a all round enjoyable blog (I also love the theme/design),
    I don’t have time to read it all at the moment but
    I have bookmarked it and also added in your RSS feeds, so when I have time I will
    be back to read more, Please do keep up the great work.

  18. Hi mates, good paragraph and nice arguments commented here, I am actually enjoying by these.

  19. Nilimwazy表示:

    https://mostbet-wre3.xyz/ о возможностях и преимуществах ставок на спорт в Mostbet – быстрые выплаты, широкая линия событий и привлекательные бонусы для новых пользователей.

  20. Greetings I am so glad I found your blog, I really found
    you by error, while I was researching on Google for something
    else, Regardless I am here now and would just like
    to say cheers for a remarkable post and a all round interesting blog (I
    also love the theme/design), I don’t have time to go through it
    all at the moment but I have book-marked it and also included your RSS feeds, so
    when I have time I will be back to read a lot more, Please do keep up
    the great job.

  21. Blog表示:

    Hello, everything is going fine here and ofcourse every one is sharing information, that’s truly good, keep up writing.

  22. JasonUnsoG表示:

    https://sweetbonanza25.com/# sweet bonanza demo oyna

  23. JasonUnsoG表示:

    https://denemebonusuverensiteler25.com/# yat?r?ms?z deneme bonusu veren siteler

發佈留言

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