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,224,296 Responses

  1. RandallTaulp表示:

    cost of propecia without prescription: order cheap propecia prices – buy propecia pills

  2. Davidtes表示:

    order generic propecia for sale cost of generic propecia without prescription buy cheap propecia online

  3. Davidtes表示:

    antibiotics cipro ciprofloxacin generic ciprofloxacin order online

  4. The 10 Most Terrifying Things About Online Grocery Stores That Ship online grocery Stores that ship

  5. Ronaldpsymn表示:

    https://cytotec.club/# buy cytotec in usa

  6. WilliamRhype表示:

    http://lisinopril.network/# zestoretic 10 12.5

  7. porn site表示:

    Heya i’m for the first time here. I came across this board
    and I to find It really useful & it helped me out much.
    I’m hoping to provide something back and help others such as
    you helped me.

  8. Davidtes表示:

    buy cytotec online fast delivery cytotec pills buy online buy cytotec over the counter

  9. RandallTaulp表示:

    how does tamoxifen work: tamoxifen breast cancer – does tamoxifen cause joint pain

  10. Gladis表示:

    10 Facts About Replacement Car Key Cost That Insists On Putting You
    In The Best Mood cheapest car key replacement (Gladis)

  11. WilliamRhype表示:

    http://nolvadex.life/# tamoxifen for men

  12. te.legra.ph表示:

    8 Tips To Enhance Your Double Glazing Lock Repair Game window repair [te.legra.ph]

  13. Seven Reasons Why New Bmw Key Is Important Replacement Key For Bmw

  14. still noct表示:

    Stilnoct is used for temporary sleep problems in adults that
    cause severe anxiety or that affect everyday life.

  15. Davidtes表示:

    should i take tamoxifen tamoxifen bone pain tamoxifen vs clomid

  16. Davidtes表示:

    lisinopril 40 mg brand name in india zestril 10 zestoretic

  17. I am not sure where you’re getting your information, but good topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for wonderful information I was looking for this info for my mission.

  18. Davidtes表示:

    buy cytotec in usa buy cytotec in usa cytotec abortion pill

  19. Davidtes表示:

    Misoprostol 200 mg buy online buy cytotec pills cytotec pills buy online

  20. Johnson表示:

    12 Stats About Double Glazed Units Near Me To Make You Think Smarter About Other
    People replacing a double glazed unit (Johnson)

  21. Are You Confident About Doing Bunk Bed Price Uk? Check This Quiz
    Bunk beds in sale

  22. Why We Do We Love Autowatch Ghost Installers Near Me (And You Should Also!) Autowatch Ghost Installers Near Me

  23. near表示:

    Where Are You Going To Find Leeds Windows And Doors One Year From
    In The Near Future? near

  24. Mazafakas.com表示:

    Is Window Repair Near The Most Effective Thing That Ever
    Was? window repair Near me (Mazafakas.com)

  25. Davidtes表示:

    buy ciprofloxacin ciprofloxacin generic price ciprofloxacin over the counter

  26. What’s The Job Market For Double Glazed Repairs Near Me Professionals?
    double Glazed Repairs Near me

  27. Ronaldpsymn表示:

    https://lisinopril.network/# order lisinopril 10 mg

  28. RandallTaulp表示:

    tamoxifen side effects forum: how to prevent hair loss while on tamoxifen – tamoxifen

  29. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 420

發佈留言

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