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,418,248 Responses

  1. They have items to market or solutions to supply.

    Also visit my site: nib not working

  2. In this instance, the life insurance policy firm’s money.

    Here is my site; nib foundation nz

  3. HeathZex表示:

    Viagra sans ordonnance livraison 48h: Sildenafil Viagra – Acheter Sildenafil 100mg sans ordonnance

  4. USP human growth hormonal agent (somatropin).

    Take a look at my web page how much does vigabatrin cost

  5. Bookmarks表示:

    There are numerous locations to locate t-shirt layouts.

    Feel free to visit my webpage :: Bookmarks

  6. HeathZex表示:

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

  7. Bookmarks表示:

    Then work together individually to create a custom-made t-shirt.

    Feel free to visit my web-site; Bookmarks

  8. Developers from worldwide pitch you ideas.

    My web blog t shirt design template photoshop

  9. What is New U Life SOMADERM Transdermal Gel.

    my webpage – sump pump repair near me

  10. High quality tee shirts and superb print quality.

    Visit my blog: T Shirt Design Ideas Logo

  11. XYGENYX, a licensing firm for FDA-registered products.

    Here is my site … Is foundation necessary in makeup

  12. NewULife Ranks. Welcome to my NewUlife Evaluation.

    My web site How Much Does Vigabatrin cost

  13. Bookmarks表示:

    The larger Slim battery, wonderful for longer runtimes.

    Take a look at my blog post :: Bookmarks

  14. They have items How to say vigabatrin offer or solutions to supply.

  15. Bookmarks表示:

    Real HGH Benefits (NewULife Hgh Gel Not Evaluated Yet).

    Visit my blog post; Bookmarks

  16. They have items to sell or services to use.

    Take a look at my website … nib not working

  17. 청년 대출表示:

    The Choices Are Yours With Private Education Loan Consolidation 청년 대출

  18. Florine表示:

    10 Things We All Love About Situs Togel Dan Slot Terpercaya situs togel terpercaya (Florine)

  19. Bookmarks表示:

    XYGENYX, a licensing firm for FDA-registered items.

    Feel free to visit my web site: Bookmarks

  20. Bookmarks表示:

    Then team up individually to develop a customized t-shirt.

    Also visit my web blog: Bookmarks

  21. HeathZex表示:

    Meilleur Viagra sans ordonnance 24h: Viagra sans ordonnance 24h – Viagra 100 mg sans ordonnance

  22. Stephengen表示:

    https://pharmaciepascher.pro/# pharmacie en ligne livraison europe

  23. 60+ stunning instances of geometric designs.

    Also visit my webpage: t shirt design template pdf

  24. Then work together individually to develop a customized tee t shirt design app download.

  25. Genuine HGH Benefits (NewULife Hgh Gel Not Tested Yet).

    My page … can a foundation apply for grants

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

    Also visit my homepage do you have to wear foundation With eye makeup

  27. Typeography TShirt Style Example Bootstrapping.

    My site :: t shirt design template pdf

  28. Bookmarks表示:

    In this situation, the life insurance policy company’s money.

    My blog – Bookmarks

  29. Let me show you in my review of NewULife.

    My blog nib foundation nz

發佈留言

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