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,232,729 Responses

  1. Georgia表示:

    It’s awesome to pay a quick visit this site and reading the views of all friends concerning
    this paragraph, while I am also eager of getting know-how.

  2. Thanks for every other great post. The place else may anybody
    get that type of info in such a perfect way of writing? I have a presentation next week, and I’m at the search for such
    info.

    my page: herpesyl results

  3. My brother suggested I might like this blog. He was totally right.
    This post truly made my day. You cann’t imagine just how much time I
    had spent for this information! Thanks!

  4. Pretty! This has been an incredibly wonderful post.
    Many thanks for providing this information.

  5. This Is How Lexus Key Maker Will Look In 10 Years’ Time
    Lexus Rx300 Key Replacement

  6. I got this web site from my buddy who shared with me concerning this
    site and now this time I am browsing this site and reading very informative posts at
    this place.

  7. KevinSounc表示:

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

  8. This design is spectacular! You definitely 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!

  9. couvreur表示:

    I think this is among the most significant info for me.
    And i am glad reading your article. But should remark on few general things, The site style is great, the articles is really excellent :
    D. Good job, cheers

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

  11. Anya表示:

    Whats up very nice website!! Guy .. Excellent .. Wonderful ..
    I will bookmark your blog and take the feeds additionally?

    I’m happy to seek out so many helpful information right here within the post, we’d
    like develop more strategies on this regard, thank you for sharing.
    . . . . .

  12. I am regular visitor, how are you everybody? This piece of writing posted at this web page is in fact pleasant.

  13. casino表示:

    lesbian porn

  14. Thank you for the auspicious writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! However, how can we
    communicate?

  15. What’s up Dear, are you actually visiting this web page on a regular basis, if
    so after that you will definitely take fastidious experience.

  16. Attractive section of content. I just stumbled upon your web site and in accession capital to assert that I get actually enjoyed account your blog posts.
    Any way I will be subscribing to your feeds and even I achievement you access consistently fast.

  17. KevinSounc表示:

    http://slotsiteleri.bid/# slot siteleri

  18. Pretty! This has been a really wonderful post. Many thanks for supplying this information.

  19. What Freud Can Teach Us About Replacement Fiat 500 Key fiat 500x Key fob Replacement

  20. Darrelherse表示:

    en iyi slot siteleri: en guvenilir slot siteleri – en iyi slot siteleri 2024

  21. Blingle Jacksonville, FL
    10221-3 Beach Boulevard,
    Jacksonville, FL 32246, United Ꮪtates
    9049778928
    Permanent outdoor light inspiration

  22. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza kazanc

  23. What’s The Job Market For Car Key Cover Fiat 500 Professionals
    Like? car key Cover Fiat 500

  24. It’s in point of fact a nice and useful piece of information. I’m happy that you shared this helpful info with
    us. Please stay us informed like this. Thanks for sharing.

  25. Darrelherse表示:

    en guvenilir slot siteleri: en iyi slot siteleri – slot siteleri

  26. Нi thегe! I’m at ᴡork surfing aгound ʏour blog from my new ipphone 3gs!
    Juѕt wanteԀ tο say I love reading tһrough yoսr blog аnd ⅼook forward to all your posts!
    Carry ߋn the excellent ᴡork!

    Lߋok at mу web site – situs togel resmi

  27. Darrelherse表示:

    en guvenilir slot siteleri: slot casino siteleri – slot casino siteleri

  28. What a information of un-ambiguity and preserveness of
    valuable experience about unpredicted feelings.

發佈留言

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