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,517,782 Responses

  1. Johnnyfut表示:

    darknet site https://github.com/darknetwebsitesgflpx/darknetwebsites – darknet markets onion address

  2. WilsonVah表示:

    Achat mГ©dicament en ligne fiable: Cialis sans ordonnance 24h – pharmacies en ligne certifiГ©es

  3. Your style is very unique in comparison to other people I have read stuff from.

    Thanks for posting when you have the opportunity, Guess I’ll just
    bookmark this site.

  4. Richardprurf表示:

    https://pharmaciemeilleurprix.shop/# pharmacie en ligne france fiable
    pharmacie en ligne fiable

  5. NorbertKep表示:

    Pharmacie en ligne livraison Europe: kamagra livraison 24h – acheter mГ©dicament en ligne sans ordonnance

  6. Diplomi_ylmt表示:

    мархи диплом купить мархи диплом купить .

  7. Richardprurf表示:

    https://kamagrameilleurprix.com/# pharmacie en ligne france livraison internationale
    Pharmacie Internationale en ligne

  8. Perryzoozy表示:

    pharmacie en ligne france pas cher Acheter Cialis pharmacie en ligne france livraison internationale

  9. WilsonVah表示:

    trouver un mГ©dicament en pharmacie: cialis prix – pharmacie en ligne avec ordonnance

  10. Richardprurf表示:

    https://tadalafilmeilleurprix.com/# acheter mГ©dicament en ligne sans ordonnance
    pharmacie en ligne sans ordonnance

  11. WilsonVah表示:

    п»їpharmacie en ligne france: Pharmacies en ligne certifiees – pharmacie en ligne france pas cher

  12. Jacobbep表示:

    Pharmacie en ligne livraison Europe http://tadalafilmeilleurprix.com/# pharmacie en ligne france livraison belgique

  13. Land 3 bonus offer signs to activate the Reward Perk Game.

    my site – online slots nz and online pokies nz

  14. In this instance, the life insurance company’s money.

    Also visit my blog post – ai detector for essays

  15. Perryzoozy表示:

    Achat mГ©dicament en ligne fiable cialis sans ordonnance pharmacie en ligne avec ordonnance

  16. Way cool! Some extremely valid points! I appreciate you penning this post plus
    the rest of the site what is a sexual assault advocate
    really good.

  17. In this case, the life insurance firm’s money.

    Also visit my site … ai detector turnitin free online

  18. Fantastic beat ! I wish to apprentice at the same time
    as you amend your website, how can i subscribe for a weblog web site?
    The account aided simple assault lawyers near me a appropriate deal.

    I had been a little bit acquainted of this your broadcast offered brilliant transparent idea.

  19. Somebody essentially help to make seriously posts I’d state.
    That is the first time I frequented your web page and to this point?

    I amazed with the analysis you made to make this particular post amazing.
    Excellent job!

    My website sexual assault lawyers philadelphia

  20. The most effective CBD items are made from high-grade hemp cbd oil for cats.

  21. Authentic HGH Benefits (NewULife Hgh Gel Not Checked Yet).

    my webpage nagad88 online casino Bangladesh login

  22. An artificial USP human development hormone (somatropin).

    My blog post :: miami corporate law

  23. Thank you, I’ve just been searching for information approximately this
    subject for ages and yours is the greatest I’ve found out so far.
    However, what concerning the conclusion? Are you positive about the source?

    Feel free to surf to my website sexual assault lawyers in charlotte nc

發佈留言

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