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,233,270 Responses

  1. WilliamRhype表示:

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

  2. 15 Things You’re Not Sure Of About Mesothelioma Settlement Mesothelioma Claim

  3. fair Slots表示:

    This Is The History Of Slot Machines fair Slots

  4. 10 Things Your Competitors Can Inform You About Search Engine Optimization Services local search Engine optimization strategy

  5. Why Incorporating A Word Or Phrase Into Your Life Will Make All The Different 9kg washing machines uk

  6. 11 “Faux Pas” You’re Actually Able To Make With Your Starter
    Kit Avon Starter Kit Avon

  7. Where Do You Think Mesothelioma Claim Be 1 Year From Today?

    Mesothelioma Lawsuit

  8. slot games表示:

    Professional Slots Explained In Fewer Than 140 Characters slot games

  9. Window Repair Near Me Tools To Improve Your Daily Lifethe
    One Window Repair Near Me Trick That Everyone Should Learn window repair near Me

  10. WilliamRhype表示:

    http://cytotec.club/# cytotec buy online usa

  11. gsnslot login表示:

    Pretty component of content. I simply stumbled upon your weblog and in accession capital to assert that I get in fact
    loved account your weblog posts. Any way I’ll be subscribing
    on your augment or even I achievement you get entry to persistently
    rapidly.

  12. You actually make it seem so easy together with your presentation but I in finding this topic to be actually
    something which I feel I’d never understand.
    It sort of feels too complex and very huge for me. I
    am having a look forward for your next submit, I will attempt
    to get the cling of it!

  13. RandallTaulp表示:

    url lisinopril hctz prescription: lisinopril 10 mg online no prescription – rx lisinopril

  14. Davidtes表示:

    buy cytotec in usa Misoprostol 200 mg buy online cytotec pills buy online

  15. vimeo.com表示:

    Online Shopping Sites List For Clothes Tips From The Top In The Industry Reusable Post-It Tabs (vimeo.com)

  16. RandallTaulp表示:

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

  17. WilliamRhype表示:

    https://finasteride.store/# order propecia without dr prescription

  18. If you are going for most excellent contents like me, just visit this website
    every day for the reason that it provides quality contents, thanks

  19. RandallTaulp表示:

    buy lisinopril 20 mg online: prescription drug prices lisinopril – lisinopril 102

  20. 15 Weird Hobbies That Will Make You More Effective At DEFRA Wood Burning Stoves Approved defra Approved clean burning stoves

  21. WilliamRhype表示:

    http://lisinopril.network/# lisinopril 50 mg price

  22. Guide To Upvc Repairs Near Me: The Intermediate Guide To Upvc Repairs Near Me Upvc Repairs Near Me

  23. The 10 Most Terrifying Things About Cerebral Palsy Compensation cerebral palsy lawsuits

  24. Heath表示:

    Why You Must Experience Best CSGO Case To Open At Least Once In Your Lifetime danger zone case – Heath

  25. Davidtes表示:

    buy ciprofloxacin cipro for sale buy cipro

  26. The Best 5kw Multi Fuel Stoves Defra Approved Tricks For Changing Your Life best 5Kw Multi fuel stove

  27. The Three Greatest Moments In Motorcycle Accident Compensation History motorcycle Accidents

  28. This Is What CSGO Case Battle Sites Will Look Like In 10 Years counter Strike global

  29. What CSGO Case Has The Best Odds Tips From The
    Top In The Industry operation broken Fang case

發佈留言

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