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,519,138 Responses

  1. Richardprurf表示:

    https://viagrameilleurprix.com/# Viagra pas cher inde
    Achat mГ©dicament en ligne fiable

  2. A synthetic USP human growth hormonal agent (somatropin).

    Visit my web page :: ai detector free best

  3. WilsonVah表示:

    vente de mГ©dicament en ligne: pharmacie en ligne sans ordonnance – pharmacie en ligne sans ordonnance

  4. What is New U Life SOMADERM Transdermal Gel.

    my web blog – ppc management agency

  5. In this instance, the life insurance business’s loan.

    Here is my web-site google ads agency

  6. In this case, the life insurance company’s loan.

    Here is my page – ppc management agency

  7. I quite like reading an article that can make men and women think.
    Also, many thanks for allowing me to comment!

    My web page; sexual assault lawyers

  8. USP human development hormone (somatropin).

    Also visit my site … lead generation agency

  9. Richardprurf表示:

    https://tadalafilmeilleurprix.shop/# pharmacie en ligne fiable
    pharmacie en ligne avec ordonnance

  10. NewULife Ranks. Invite to my NewUlife Review.

    my site … ai detector quillbot

  11. I don’t even know the way I ended up right here, but
    I thought this publish was once great. I don’t recognize who you might be but definitely you’re
    going to a well-known blogger if you happen to are not already 😉 Cheers!

    Here is my webpage :: sexual assault lawyers near me

  12. The best CBD items are made from high-grade hemp cbd drops For cats.

  13. WilsonVah表示:

    Pharmacie sans ordonnance: pharmacie en ligne france – acheter mГ©dicament en ligne sans ordonnance

  14. Real HGH Rewards (NewULife Hgh Gel Not Examined Yet).

    My blog post … ai detector for teachers

  15. They have products to market or services to provide.

    Here is my blog post ai detector turnitin free online

  16. What is New U Life SOMADERM Transdermal Gel.

    my site … ai detector free grammarly

  17. Finding an accurate equity release vs secured loan release quote can be tough.

  18. Hi there everybody, here every one is sharing these kinds of knowledge,
    so it’s pleasant to read this web site, and I used to pay a quick
    visit this web site daily.

    Take a look at my webpage – sexual assault charges in texas

  19. Precisely what I was searching for, appreciate it for putting
    up.

    Have a look at my web blog :: sexual assault lawyers in charlotte nc

  20. NewULife Ranks. Invite to my NewUlife Testimonial.

    Also visit my page – Ai Detector free

  21. Land 3 bonus symbols to turn on the Jackpot Incentive Video Game.

    My homepage :: online slots nz and online pokies nz

發佈留言

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