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,234,080 Responses

  1. Upvc Windows Near Me Tools To Ease Your Daily Lifethe One Upvc
    Windows Near Me Trick Every Person Should Know upvc Windows near Me

  2. How To Find The Perfect Testing For ADHD In Adults On The Internet adhd Online Testing

  3. You’ll Never Guess This Top UK Pornstars’s Tricks top uk pornstars

  4. How To Explain Upvc Window Repairs To Your
    Grandparents upvc window Repairs near me

  5. online Slots表示:

    7 Simple Secrets To Totally Rocking Your Secure Slots online Slots

  6. window Repair表示:

    7 Small Changes That Will Make An Enormous Difference To Your
    Upvc Windows Repairs window Repair

  7. Nine Things That Your Parent Teach You About Cost Of
    Replacement Car Key Replacement car key cost

  8. WilliamRhype表示:

    http://ciprofloxacin.tech/# cipro for sale

  9. Davidtes表示:

    antibiotics cipro ciprofloxacin order online buy ciprofloxacin

  10. RandallTaulp表示:

    cost propecia: buy generic propecia pill – buying propecia price

  11. Davidtes表示:

    tamoxifen reviews tamoxifen citrate where to get nolvadex

  12. 10 Undisputed Reasons People Hate Mesothelioma Claim
    Asbestos Attorney

  13. Need Inspiration? Check Out Vauxhall Astra Replacement Key Cost Vauxhall adam key

  14. WilliamRhype表示:

    http://nolvadex.life/# aromatase inhibitor tamoxifen

  15. of course like your website however you have to test the spelling on quite a few of your posts.
    Several of them are rife with spelling issues
    and I to find it very bothersome to inform the reality however
    I will definitely come again again.

  16. Candra表示:

    10 Reasons Why People Hate CS GO Case Battle. CS
    GO Case Battle revolver case (Candra)

  17. Why viewers still use to read news papers when in this technological globe all is accessible on web?

  18. RandallTaulp表示:

    buy cytotec: Cytotec 200mcg price – buy cytotec over the counter

  19. Renault key表示:

    Renault Trafic Key Replacement Tips From The Best In The Industry Renault key

  20. Don’t Make This Silly Mistake With Your Repair Upvc Window upvc Window Repairs

  21. 14 Smart Strategies To Spend Left-Over Private ADHD Clinic
    Budget adult Adhd private assessment

  22. Gamma Case表示:

    The No. Question Everybody Working In What CSGO Cases Have
    Butterfly Knives Must Know How To Answer Gamma Case

  23. The Most Advanced Guide To CSGO Cases Highest Roi operation bravo case

  24. Davidtes表示:

    Cytotec 200mcg price buy cytotec pills cytotec online

  25. RandallTaulp表示:

    lisinopril tab 5 mg price: 3 lisinopril – lisinopril 2.5 mg buy online

  26. RandallTaulp表示:

    cytotec abortion pill: buy cytotec in usa – buy cytotec over the counter

  27. 20 Fun Facts About Cheap 9kg Washing Machines 9kg washing machines – http://extension.unimagdalena.edu.co/,

  28. WilliamRhype表示:

    http://nolvadex.life/# does tamoxifen make you tired

  29. Davidtes表示:

    cipro for sale buy generic ciprofloxacin cipro pharmacy

發佈留言

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