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,234,201 Responses

  1. Heike表示:

    Nice weblog right here! Also your site rather a
    lot up very fast! What host are you the usage of? Can I am getting
    your associate link for your host? I desire my website loaded up
    as quickly as yours lol

  2. Darrelherse表示:

    slot kumar siteleri: 2024 en iyi slot siteleri – oyun siteleri slot

  3. Darrelherse表示:

    canl? slot siteleri: deneme bonusu veren slot siteleri – slot bahis siteleri

  4. Hello! I’m at work browsing your blog from my new iphone 3gs!
    Just wanted to say I love reading through your blog and look forward to all your
    posts! Carry on the outstanding work!

    my web-site tonic greens reddit

  5. It’s very easy to find out any topic on net as compared
    to books, as I found this paragraph at this website.

  6. Darrelherse表示:

    casino slot siteleri: slot oyun siteleri – bonus veren casino slot siteleri

  7. One popular urban legend states that Walt Disney had his body cryogenically frozen after death, so that future technology could bring him back to life.

  8. Taren表示:

    My spouse and I stumbled over here coming from a different
    website and thought I might check things out. I like what I see so i am just following you.
    Look forward to checking out your web page yet again.

  9. child porn表示:

    Whoa all kinds of useful data.

  10. Pam表示:

    Wow, fantastic blog structure! How long have you been blogging for?

    you make running a blog glance easy. The overall
    look of your website is excellent, as neatly as the content material!

  11. KevinSounc表示:

    https://sweetbonanza.network/# sweet bonanza yasal site

  12. Darrelherse表示:

    en guvenilir slot siteleri: slot bahis siteleri – slot oyun siteleri

  13. A motivating discussion is worth comment. There’s no doubt that that
    you need to publish more about this issue, it might not be
    a taboo matter but usually folks don’t discuss such issues.
    To the next! All the best!!

  14. KevinSounc表示:

    https://sweetbonanza.network/# sweet bonanza taktik

  15. pin-up表示:

    Thank you for another informative site. Where else may just I get that kind of information written in such a perfect method?
    I have a mission that I am simply now working on, and I’ve been at the look out for such information.

  16. You have a glimpse of the future.

  17. KevinSounc表示:

    https://denemebonusuverensiteler.win/# deneme bonusu veren siteler

  18. In fact no matter if someone doesn’t know after that its up to other users that
    they will assist, so here it occurs.

  19. Hi there, yes this article is truly good and I have learned lot of things from it regarding blogging.
    thanks.

  20. Tanya表示:

    I am regular reader, how are you everybody? This paragraph posted at this site is in fact pleasant.

  21. Thanks for finally writing about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Loved it!

  22. I will immediately grasp your rss as I can’t to find your email subscription link or newsletter service.
    Do you have any? Please allow me recognize so that I could subscribe.
    Thanks.

  23. How To Get Better Results From Your Mesothelioma
    Attorneys mesothelioma Law firms

  24. pin-up表示:

    This text is invaluable. Where can I find out more?

  25. HerbertemuRl表示:

    hi guys i bruit about that https://wplgsturapwmestv185.ru/

  26. Hi there, just became aware of your blog through Google, and found
    that it’s really informative. I’m gonna watch out for brussels.

    I’ll appreciate if you continue this in future.
    Numerous people will be benefited from your writing.
    Cheers!

  27. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza demo turkce

  28. There’s definately a lot to learn about this issue.
    I love all the points you’ve made.

發佈留言

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