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,467,567 Responses

  1. Mighty Dog Roofing
    Reimer Drive North 13768
    Maple Grove, MN55311 United Ꮪtates
    (763) 280-5115
    siding repair pros

  2. Herman Horton表示:

    Valuable information! Find more at عمل پی ار کی

  3. Very good info. Lucky me I recently found your site
    by accident (stumbleupon). I’ve book-marked it for later!

  4. JasonUnsoG表示:

    http://slotsiteleri25.com/# az parayla cok kazandiran slot oyunlar?

  5. 익산호빠表示:

    익산에서 제일 잘 나가는 곳이 저희 익산호빠입니다.

  6. Norman Duncan表示:

    Thanks for the detailed post. Find more at lawn maintenance near me

  7. FrankEmine表示:

    en cok kazand?ran slot oyunlar?: guvenilir slot siteleri – slot oyunlar? puf noktalar?

  8. Belle Bennett表示:

    This was very insightful. Check out رنگ ضد حریق for more

  9. developer web表示:

    Currently it sounds like BlogEngine is the top blogging platform out there
    right now. (from what I’ve read) Is that what you’re using on your blog?

  10. Marc Bowman表示:

    If you’re dealing with chronic pain, I suggest finding a ##Puyallup Chiropractor## to help manage it effectively Puyallup Chiropractor

  11. Double Glazing Hinges: What’s New? No One Is Talking About
    Casement Window hinge replacement

  12. Wonderful blog! Do you have any recommendations for aspiring writers?
    I’m hoping to start my own site soon but I’m a little lost on everything.

    Would you advise starting with a free platform like WordPress or go for a paid option? There are so
    many options out there that I’m totally overwhelmed ..
    Any recommendations? Thanks a lot!

  13. Wallacefex表示:

    en cok kazand?ran slot oyunlar?: az parayla cok kazandiran slot oyunlar? – en kazancl? slot oyunlar?

  14. I recently started visiting a Puyallup chiropractor for adjustments, and it has made a world of difference in my overall well-being Puyallup Chiropractor

  15. Ian Herrera表示:

    The idea of a bug-free world sounds like a dream come true for me! exterminator near me

  16. I want to to thank you for this excellent read!!
    I certainly loved every little bit of it. I have you book marked to look at new stuff you post…

  17. Niamh表示:

    Aw, this was a very nice post. Spending some time and actual effort to make a good article… but what can I
    say… I hesitate a whole lot and never manage to get
    nearly anything done.

  18. FrankEmine表示:

    sweet bonanza: sweet bonanza demo oyna – sweet bonanza oyna

  19. Todd Lee表示:

    Very useful post. For similar content, visit Trailers for sale

  20. Hello there, I think your blog could possibly be having internet browser compatibility issues.
    Whenever I take a look at your site in Safari, it looks fine but when opening in IE,
    it’s got some overlapping issues. I simply wanted to provide you with a quick heads up!
    Other than that, fantastic site!

  21. How To Solve Issues Related To Best 4 Wheel Mobility Scooter Best mobility scooters Uk

  22. How To Outsmart Your Boss In Evolution Baccarat Free Experience 에볼루션 바카라 체험 (Beetlekendo5.Bravejournal.net)

  23. This design is incredible! You certainly know how to keep a reader
    amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job.
    I really enjoyed what you had to say, and more than that, how you presented it.

    Too cool!

  24. Wallacefex表示:

    sweet bonanza: sweet bonanza demo oyna – sweet bonanza guncel

  25. Thanks for the useful post. More like this at AvraDentist

發佈留言

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