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,141,228 Responses

  1. DavidOwelo表示:

    http://cenforce.pro/# order cenforce

  2. ElmerWes表示:

    Purchase Cenforce Online cheapest cenforce cenforce.pro

  3. JamesNow表示:

    buy Viagra online: Cheapest place to buy Viagra – over the counter sildenafil

  4. ElmerWes表示:

    Viagra without a doctor prescription Canada viagras.online viagra without prescription

  5. TylerPhona表示:

    https://viagras.online/# buy viagra here

  6. Dyan表示:

    The 3 Greatest Moments In Michael Kors Bag Crossbody History michael kors bags black friday – Dyan,

  7. GeorgeSep表示:

    cheapest cialis: cialist.pro – Cialis over the counter

  8. 10 Tell-Tale Signals You Need To Find A New Double Glazing Unit Repair Double Glazing Repairs, Apricot-Pineapple-Fc3Krn.Mystrikingly.Com,

  9. 10 Methods To Build Your Window Repair Leeds Empire Upvc Window repairs leeds

  10. TylerPhona表示:

    http://kamagra.win/# Kamagra 100mg

  11. The Most Innovative Things That Are Happening With Minnesota Birth Injury Attorneys Maternal Birth Injury

  12. 10 Misconceptions That Your Boss May Have Regarding 10kg Washing Machine Price 10 kg washing machine

  13. Lyle表示:

    10 Facts About Upvc Window Repairs That Will Instantly Put You In Good Mood window repairs near me (Lyle)

  14. mt5 plugin表示:

    I like the valuable information you supply to your articles.
    I’ll bookmark your blog and take a look at once more right here regularly.
    I am relatively sure I’ll be told a lot of new stuff right here!

    Good luck for the following!

    Feel free to visit my blog post mt5 plugin

  15. Ask Me Anything: 10 Responses To Your Questions About Locksmith For Cars Near Me locksmith for cars near me [k12.instructure.com]

  16. 10 Window Glass Repair Near Me Tips All Experts Recommend double glazed window repairs Near me

  17. CharlesDog表示:

    https://viagras.online/# buy viagra here

  18. ElmerWes表示:

    Cheap Sildenafil 100mg Cheap Viagra 100mg viagra without prescription

  19. Automobile Locksmiths Tools To Help You Manage
    Your Daily Lifethe One Automobile Locksmiths Trick That Every Person Must Be Able To Automobile Locksmiths

  20. 20 Things Only The Most Devoted Double Glazing Condensation Repair Cost Fans Should Know window replacement near Me

  21. Gudang138表示:

    Hi there everyone, it’s my first pay a quick visit at this web page,
    and piece of writing is really fruitful in favor of me,
    keep up posting these types of articles.

  22. Isidro表示:

    17 Reasons Not To Ignore Symptoms Of Adhd In Adults Female undiagnosed adhd in adults symptoms;
    Isidro,

  23. CharlesDog表示:

    https://cenforce.pro/# Buy Cenforce 100mg Online

  24. The 10 Most Scariest Things About Leeds Double Glazing leeds double glazing

  25. JamesNow表示:

    cheap viagra: viagras.online – Viagra tablet online

  26. CharlesDog表示:

    https://cenforce.pro/# cheapest cenforce

  27. Replacement表示:

    10 Things Everyone Gets Wrong About The Word “Window Sash Repairs” Replacement

  28. 10 Healthy Habits To Use Local Window Repair window doctor Near Me

發佈留言

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