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,196,441 Responses

  1. kaymell.uk表示:

    10 Life Lessons We Can Learn From Slots kaymell.uk

  2. центр ремонта стиральных машин http://www.centr-remonta-stiralnyh-mashin.ru .

  3. Thank you for the good writeup. It in reality
    used to be a amusement account it. Look complicated to more brought
    agreeable from you! By the way, how could we keep up a correspondence?

  4. I’m very pleased to discover this web site. I want to to thank you for your time
    due to this wonderful read!! I definitely loved every little
    bit of it and i also have you saved to fav to see new things on your
    site.

  5. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://onkopaednki.de/jkkqm of type and scrambled it to make a type specimen book.

  6. центр ремонта стиральных машин https://centr-remonta-stiralnyh-mashin.ru .

  7. The Strategies For Availing A Home Loan 디딤돌 대출

  8. Medicine information. Generic Name.
    how to get cheap zanaflex without prescription
    All trends of medicine. Read here.

  9. сервис ремонта стиральных машин https://centr-remonta-stiralnyh-mashin.ru .

  10. cassylawn表示:

    Ten Causes Of Mesothelioma Other Than Asbestoss That Really Make Your
    Life Better cassylawn

  11. мастерская по ремонту смартфонов мастерская по ремонту смартфонов .

  12. сервис ремонта стиральных машин http://www.centr-remonta-stiralnyh-mashin.ru .

  13. выезд мастера для ремонта стиральной машины http://www.centr-remonta-stiralnyh-mashin.ru/ .

  14. Medicine information leaflet. Long-Term Effects.
    get neurontin tablets
    Best news about pills. Read here.

  15. Hello, I enjoy reading through your article post. I like to
    write a little comment to support you.

  16. Приветствую. Может кто знает, где почитать полезные блоги о недвижимости? Сейчас читаю https://u-mechanik.ru

  17. Приветствую. Подскажите, где найти разные блоги о недвижимости? Сейчас читаю https://u-mechanik.ru

  18. Hi there this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or if you
    have to manually code with HTML. I’m starting a blog soon but
    have no coding expertise so I wanted to get advice from someone with
    experience. Any help would be greatly appreciated!

  19. Good day! I could have sworn I’ve been to this site before but after browsing through some of the post
    I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be bookmarking and checking back often!

  20. It is the best time to make some plans for the future and it’s time to be happy.
    I’ve read this post and if I could I wish to suggest you few interesting things or suggestions.
    Maybe you could write next articles referring to this article.
    I wish to read even more things about it!

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

  22. HL8表示:

    Excellent blog here! Also your website loads up very fast!
    What host are you using? Can I get your affiliate link to your host?
    I wish my website loaded up as quickly as yours lol

  23. ремонт стиральных машин дома на выезде https://centr-remonta-stiralnyh-mashin.ru .

  24. How Much Interest Will I Pay On The Car Bank? 100만원 대출 (futureendeavorsinc.com)

  25. I have been surfing on-line greater than 3 hours as of late, yet I
    never discovered any fascinating article like yours.
    It is beautiful value enough for me. In my view, if all web owners
    and bloggers made excellent content material as you probably did, the internet will likely
    be much more helpful than ever before.

  26. Всем привет! Подскажите, где почитать полезные блоги о недвижимости? Пока нашел https://u-mechanik.ru

  27. I need to to thank you for this excellent
    read!! I absolutely enjoyed every bit of it. I have got you saved as a favorite
    to look at new things you post…

發佈留言

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