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...

987,730 Responses

  1. fun slots表示:

    20 Myths About Popular Slots: Dispelled fun slots

  2. Medicament information sheet. Generic Name.
    can you buy generic zofran no prescription
    All what you want to know about drugs. Get now.

  3. Thca Flower表示:

    Hi, i think that i noticed you visited my site thus i came to ?return the want?.I’m attempting to to find things to enhance my site!I suppose its ok to make use of a few of your ideas!!

  4. sitesco.ru表示:

    9 . What Your Parents Taught You About Best
    Online Clothing Sites Uk Best Online clothing Sites uk (sitesco.ru)

  5. Drugs information sheet. Short-Term Effects.
    can i purchase generic rizatriptan without rx
    Some what you want to know about drug. Get now.

  6. Blythe表示:

    20 Trailblazers Leading The Way In Slot Wins slots with bonuses [Blythe]

  7. Juliane表示:

    9 Things Your Parents Taught You About Mesothelioma Lawsuits mesothelioma lawsuit, Juliane,

  8. Quentin表示:

    Responsible For A Upvc Window Repair Near Me Budget?
    12 Top Ways To Spend Your Money upvc window repairs near me (Quentin)

  9. growth matrix表示:

    bookmarked!!, I really like your web site!

    Have a look at my blog – growth matrix

  10. Are You In Search Of Inspiration? Try Looking Up Low Limit Slots Best payouts slots

  11. My brother recommended I would possibly like this blog.
    He was entirely right. This post actually made my day.
    You can not imagine simply how much time I had spent for this info!
    Thank you!

  12. bonus slots表示:

    10 Best Mobile Apps For Slot Bonuses bonus slots

  13. The 10 Scariest Things About Truck Accident Law Firm Truck accident Law firm

  14. nolvadex rx表示:

    Drugs prescribing information. Long-Term Effects.
    nolvadex rx
    Everything information about meds. Get now.

  15. Christina表示:

    How To Make A Successful Upvc External Doors Instructions For Homeschoolers From Home upvc
    doors price – Christina,

  16. Your means of describing all in this article is
    actually pleasant, all be able to simply know it, Thanks a
    lot.

  17. Drug prescribing information. Brand names.
    can i order aldactone without insurance
    Actual information about meds. Get information now.

  18. Winning slots表示:

    You’ll Never Guess This Winning Slots’s Benefits Winning slots

  19. It is important that attorneys know how to identify Asbestos attorney products in every case.
    This can be accomplished by talking to colleagues, collecting
    documents, or analyzing samples taken from home or workplaces.

  20. Speak “Yes” To These 5 Slot Mechanics Tips slot Machine games

  21. How To Beat Your Boss On Double Glazing Supplies Near
    Me double glazed glass units, m.thehaedam.com,

  22. Verla表示:

    A fascinating discussion is worth comment. There’s no doubt that that you should write more about this topic,
    it may not be a taboo subject but usually people don’t discuss
    such issues. To the next! Kind regards!!

  23. I am regular visitor, how are you everybody? This paragraph posted at this site is in fact good.

  24. go表示:

    Definitely consider that that you stated. Your favourite justification seemed to be
    on the net the easiest thing to have in mind of. I say to
    you, I certainly get annoyed while other folks consider worries that they just don’t realize
    about. You managed to hit the nail upon the highest
    and also defined out the entire thing without having
    side-effects , other people can take a signal. Will probably be back to get more.
    Thank you

  25. textpert.hu表示:

    What’s The Job Market For Which Is The Best Online Supermarket Professionals?
    Which is the best Online supermarket (textpert.hu)

發佈留言

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