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,232,026 Responses

  1. What’s The Current Job Market For Mazda 2 Key Fob Replacement
    Professionals Like? mazda 2 Key Fob

  2. cs20 case表示:

    10 Things We All Do Not Like About Which CSGO Case Is The Most
    Profitable cs20 case

  3. fit表示:

    20 Questions You Need To Ask About Double.Glazing Near Me Prior To Purchasing Double.Glazing Near Me fit

  4. JamesNow表示:

    sildenafil online: Buy Viagra online cheap – Buy Viagra online cheap

  5. brown表示:

    It’s A Michael Kors Bag Success Story You’ll Never Remember brown

  6. ElmerWes表示:

    buy kamagra online usa kamagra oral jelly Kamagra Oral Jelly

  7. Gigatree.Eu表示:

    5 Reasons To Consider Being An Online Shopping Online And 5 Reasons Not To Online Shopping
    (Gigatree.Eu)

  8. ElmerWes表示:

    Kamagra 100mg price kamagra oral jelly Kamagra 100mg

  9. You’ll Never Guess This Private Adhd Assessment Near Me’s Secrets Adhd assessment near me

  10. Why We Why We Veleco Scooter (And You Should Too!) http://www.Mymobilityscooters.uk

  11. JamesNow表示:

    Generic Cialis price: buy cialis online – Tadalafil price

  12. DavidOwelo表示:

    https://viagras.online/# Sildenafil Citrate Tablets 100mg

  13. Hi, this weekend is fastidious designed for me, as this time i am reading this great educational post here at my house.

    My webpage: mens golf shoes

  14. The Step-By -Step Guide To Choosing Your Bean-To-Cup Machine Bean To Cup Machines

  15. CharlesDog表示:

    http://kamagra.win/# Kamagra tablets

  16. If you wish for to get much from this post then you have to apply these methods to your
    won weblog.

  17. avon So Soft表示:

    This History Behind Skin So Soft Dry Oil Spray Can Haunt You Forever!
    avon So Soft

  18. GeorgeSep表示:

    Levitra online pharmacy: Buy Vardenafil 20mg – Levitra 10 mg best price

  19. syair sdy表示:

    Very quickly this web site will be famous amid all blogging visitors, due to it’s fastidious content

  20. JamesNow表示:

    Cenforce 100mg tablets for sale: cheapest cenforce – cheapest cenforce

  21. Nearest表示:

    The People Nearest To Adult Toy For Men Tell You Some Big Secrets Nearest

  22. The 10 Most Terrifying Things About Heavy Duty Electric Wheelchair
    Heavy duty electric Wheelchairs

  23. 10 Things You Learned In Kindergarden They’ll Help You Understand Online Shopping
    Sites Clothes Cheap breathable Patio Cover

  24. The Biggest Sources Of Inspiration Of Best CSGO
    Case To Open operation broken Fang case

  25. JamesNow表示:

    Cialis without a doctor prescription: Generic Tadalafil 20mg price – Tadalafil Tablet

  26. Vimeo表示:

    10 Signs To Watch For To Look For A New France Online Shopping Sites Clothes Vimeo

  27. 7 Secrets About Birth Injury Case That No One Will
    Tell You scottsboro birth Injury lawsuit

發佈留言

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