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

  1. American Fridge Freezer Sale: 11 Things You’re
    Not Doing smallest american fridge freezer

  2. This Is The Myths And Facts Behind Car Diagnostic diagnostics Check

  3. Dorine表示:

    You’ll Never Guess This Cheap Fridge Freezers’s Benefits cheap fridge freezers (Dorine)

  4. WilliamRhype表示:

    https://finasteride.store/# propecia cost

  5. Fakenews.win表示:

    The Reason Why Adding A Double Glazed Windows Repairs To Your Life’s Activities Will Make All The Difference Window
    Repairs (Fakenews.win)

  6. 15 Ideas For Gifts For Your Secondary Double Glazing Near Me Lover In Your Life Double Glazing Window Replacement

  7. MichaelZek表示:

    фулфилмент вайлдберриз https://24fulfilment-marketplace.ru/

  8. rajabaji表示:

    Way cool! Some very valid points! I appreciate you penning this article and the rest of the site is also very
    good.

  9. 17 Reasons Why You Shouldn’t Not Ignore Small Double Mattresses Bunk Bed Mattresses

  10. Carson表示:

    A Provocative Remark About Online Shopping Top 7 5W-30 Conventional Motor Oil (Carson)

  11. 5 Killer Quora Answers To Mesothelioma Claim
    Mesothelioma litigation

  12. MichaelZek表示:

    фулфилмент москва вайлдберриз https://24fulfilment-marketplace.ru/

  13. Are You Tired Of Double Glazing Deals Near Me?
    10 Inspirational Ideas To Bring Back Your Passion double glazing window handles

  14. 10-Pinterest Accounts You Should Follow About Bmw Replacement Key Cost bmw replacement Key cost uk

  15. Five Tools That Everyone Who Works In The Malpractice Legal Industry Should
    Be Utilizing malpractice lawsuits

  16. MichaelZek表示:

    фулфилмент озон https://24fulfilment-marketplace.ru/

  17. Dating表示:

    A fascinating discussion is worth comment. I do think
    that you should publish more about this subject, it might not be a taboo subject but typically people do
    not speak about such topics. To the next! Cheers!!

  18. موزیک表示:

    Paragraph writing is also a excitement, if you know afterward you can write if not it
    is difficult to write.

  19. repairs表示:

    10 Simple Ways To Figure Out Your Double Glazing
    Companies Near Me repairs

  20. Wow that was odd. I just wrote an extremely long comment
    but after I clicked submit my comment didn’t appear.

    Grrrr… well I’m not writing all that over again. Anyway, just wanted to say superb blog!

  21. RandallTaulp表示:

    buy cytotec over the counter: buy cytotec – cytotec pills buy online

  22. MichaelZek表示:

    услуги фулфилмента в москве https://24fulfilment-marketplace.ru/

  23. 10 Best Facebook Pages Of All Time About Automotive Door Lock Repair Car lock Repairs near me

  24. Case glove表示:

    Will CSGO Case Prices Go Down Tools To Improve
    Your Daily Lifethe One Will CSGO Case Prices Go Down Trick That Should Be Used By Everyone Be Able To Case glove

  25. herpesyl表示:

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.

    Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

  26. RandallTaulp表示:

    lisinopril 240: lisinopril buy without prescription – lisinopril 10 mg tablet price

發佈留言

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