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,230,580 Responses

  1. RandallTaulp表示:

    tamoxifen and depression: tamoxifen rash – tamoxifen vs raloxifene

  2. Vimeo.Com表示:

    “Ask Me Anything”: Ten Answers To Your Questions About Shopping Online Uk Vimeo.Com

  3. Jina表示:

    See What Replacement Nissan Key Fob Tricks The Celebs Are Making Use Of replacement nissan key fob; Jina,

  4. WilliamRhype表示:

    https://lisinopril.network/# drug prices lisinopril

  5. 10 Of The Top Mobile Apps To Mesothelioma mesothelioma Law

  6. RandallTaulp表示:

    ciprofloxacin generic: buy cipro – buy generic ciprofloxacin

  7. Davidtes表示:

    cytotec buy online usa buy cytotec over the counter buy cytotec over the counter

  8. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 5 mg price

  9. Ronaldpsymn表示:

    http://cytotec.club/# purchase cytotec

  10. case opening表示:

    Why You Should Be Working With This CSGO Cases Opening
    Sites case opening

  11. Why Everyone Is Talking About Ghost Immobiliser Near Me This Moment
    ghost 2 Immobiliser cost

  12. RandallTaulp表示:

    buying generic propecia without dr prescription: buying generic propecia without insurance – cost propecia without dr prescription

  13. Five Qualities That People Search For In Every Window Repair Near Upvc window repair near me

  14. What’s The Current Job Market For Search Engine Optimization Services
    Professionals Like? search engine optimization Services

  15. WilliamRhype表示:

    https://finasteride.store/# buying generic propecia without dr prescription

  16. Davidtes表示:

    how does tamoxifen work tamoxifen 20 mg tablet benefits of tamoxifen

  17. Rentry.co表示:

    How To Save Money On Efoldi Mobility Scooter For Sale veleco
    mobility scooter for sale; Rentry.co,

  18. See What Veleco Mobility Scooter For Sale Tricks The Celebs
    Are Utilizing veleco Mobility scooter for sale

  19. Ronaldpsymn表示:

    http://cytotec.club/# purchase cytotec

  20. 10 Facts About Treadmill Folding Incline That Will Instantly Put You In A Positive Mood small Treadmill incline, http://Www.huenhue.Net,

  21. WilliamRhype表示:

    https://cytotec.club/# buy cytotec online

  22. Guide To Double Glazed Units Near Me: The Intermediate Guide In Double Glazed Units
    Near Me double glazed Units Near me

  23. WilliamRhype表示:

    https://ciprofloxacin.tech/# buy cipro online

  24. Case Falchion表示:

    20 Things You Need To Know About Will CSGO Cases Go Up In Price Case Falchion

  25. Excellent weblog right here! Also your site lots up fast!

    What web host are you the use of? Can I get your associate hyperlink in your host?
    I want my web site loaded up as fast as yours lol

  26. RandallTaulp表示:

    lisinopril sale: lisinopril tablets india – lisinopril 20 mg for sale

發佈留言

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