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,233,518 Responses

  1. Window Repair表示:

    11 “Faux Pas” That Are Actually Okay To Create Using Your
    Repair Double Glazing Window Window Repair

  2. Excellent website you have here but I was wondering if you knew
    of any user discussion forums that cover the same topics discussed in this article?

    I’d really like to be a part of community where I can get comments from other
    experienced people that share the same interest. If you have any suggestions,
    please let me know. Bless you!

  3. Mohammad表示:

    What’s The Good And Bad About Tommy Hilfiger Bag Tommy Hilfiger hand bags; Mohammad,

  4. Marcelabelp表示:

    https://mexicoph24.life/# medication from mexico pharmacy

  5. Rickysem表示:

    best online canadian pharmacy canadian pharmacies canadian family pharmacy

  6. Wisdom On Electric Wall Fireplace From An Older Five-Year-Old Electric fireplace wall
    (marvelvsdc.faith)

  7. The 10 Scariest Things About Double Glazing Near Me Double Glazing Near Me

  8. The Largest Issue That Comes With Electric Mobility
    Scooters Near Me, And How You Can Solve It mobility
    scooter Near me for sale (extension.unimagdalena.edu.co)

  9. Rickysem表示:

    reputable indian online pharmacy Cheapest online pharmacy online pharmacy india

  10. JustinCit表示:

    reputable indian pharmacies https://indiaph24.store/# п»їlegitimate online pharmacies india
    indian pharmacy online

  11. List.Ly表示:

    Concert Attire Stamford
    360 Fairfield Ave,
    Stamford, CT 06902, United Ѕtates
    +12033298603
    Stage Ꮮeft (List.Ly)

  12. Rolando表示:

    15 Terms That Everyone Involved In Double Bunk Beds Top And Bottom Industry Should Know full over full bunk beds (Rolando)

  13. package表示:

    What Is What Is Tier 2 Backlinks? History Of What Is
    Tier 2 Backlinks package

  14. What Are The Biggest “Myths” About General Psychiatric Assessment Could Be A Lie Adhd
    Psychiatric Assessment (http://Www.40Billion.Com)

  15. JustinCit表示:

    Online medicine order http://indiaph24.store/# world pharmacy india
    best online pharmacy india

  16. Rickysem表示:

    canadian pharmacy uk delivery Licensed Canadian Pharmacy canadian drugs

  17. Marcelabelp表示:

    https://indiaph24.store/# indianpharmacy com

  18. lottopoon888表示:

    Hello friends, its wonderful piece of writing about tutoringand completely explained, keep it up all the time.

  19. Marcelabelp表示:

    https://mexicoph24.life/# best online pharmacies in mexico

  20. Rickysem表示:

    world pharmacy india indian pharmacy fast delivery indianpharmacy com

  21. Marcelabelp表示:

    http://canadaph24.pro/# canadian pharmacy 1 internet online drugstore

  22. Michaelcew表示:

    https://mexicoph24.life/# reputable mexican pharmacies online

  23. One Key Trick Everybody Should Know The One Tommy Hilfiger Cross Bag
    Trick Every Person Should Be Able To Tommy hilfiger Bags Sale

  24. vacsatc.Hu表示:

    Sell Products At Home Tips From The Most Successful In The Industry who
    sell avon near me (vacsatc.Hu)

  25. 15 Rabbit Vibrator Online Benefits Everybody Must Know rabbit Vibrators

  26. Rickysem表示:

    best online pharmacies in mexico Online Pharmacies in Mexico best online pharmacies in mexico

  27. What’s The Current Job Market For Boat Accident Attorney Professionals Like?
    boat Accident attorney

  28. Rickysem表示:

    indianpharmacy com world pharmacy india india pharmacy

  29. 20 Best Tweets Of All Time Replacement Windows Leeds Upvc Door Repair Roundhay

  30. 7 Simple Tricks To Moving Your Best price Beko washing machines;
    bookmarkzones.trade,

發佈留言

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