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,665,279 Responses

  1. The No. 1 Question Everyone Working In Asbestos Mesothelioma Lawsuit Should Be Able To Answer mesothelioma Attorneys

  2. hihi表示:

    Hey there! This post couldn’t be written any better!
    Reading this post reminds me of my previous room mate!

    He always kept talking about this. I will forward this post to him.
    Fairly certain he will have a good read. Thank you for sharing!

  3. Hi there! I’m at work surfing around your blog from my new apple iphone!
    Just wanted to say I love reading your blog and look forward to
    all your posts! Carry on the outstanding work!

  4. StevenNub表示:

    priligy maxpharm: priligy max pharm – dapoxetine online

  5. Inspiring quest there. What occurred after?

    Take care!

  6. Danieljek表示:

    buy amoxicillin online with paypal http://priligymaxpharm.com/# buy dapoxetine online

  7. Danieljek表示:

    amoxicillin 500mg capsule https://amoxilcompharm.com/# generic amoxil 500 mg

  8. WOW just what I was looking for. Came here by searching for strong cardboard boxes

  9. xxx表示:

    lgbt porn

  10. Blakesmema表示:

    заказать кухню — Сделайте заказ на кухню своей мечты с доставкой.

  11. Danieljek表示:

    amoxicillin where to get https://prednisoneraypharm.com/# generic prednisone pills

  12. I’m not that much of a online reader to be honest but your sites really nice,
    keep it up! I’ll go ahead and bookmark your website to
    come back later. Cheers

  13. BrandenSlomy表示:

    priligy: priligy – priligy maxpharm

  14. 15 Best Accident Lawyer Near Me Bloggers You Should Follow Best accident lawyer near me

  15. где-то потребуется профильная подготовка (например, http://demo.4admins.ru/viewtopic.php?f=10&t=2160 диплом педагогического вуза).

  16. Bradleygoath表示:

    buy amoxicillin 500mg canada amoxicillin 500mg capsules antibiotic amoxicillin online no prescription

  17. BrandenSlomy表示:

    priligy max pharm: dapoxetine price – Priligy tablets

  18. Bradleygoath表示:

    priligy Priligy tablets priligy max pharm

  19. BrandenSlomy表示:

    where to buy cheap clomid online: clomid online – cost of clomid without prescription

  20. Danieljek表示:

    amoxicillin tablet 500mg https://clomidrexpharm.com/# cost of generic clomid pills

  21. upvc表示:

    Guide To Repair Upvc Window: The Intermediate Guide The Steps To Repair Upvc Window upvc

  22. Medicines information sheet. What side effects?
    where to buy setraline
    All what you want to know about drugs. Read information now.

  23. Bradleygoath表示:

    can i order clomid for sale clomid purchase online rex pharm can you get clomid for sale

  24. Danieljek表示:

    amoxicillin discount coupon https://clomidrexpharm.com/# can i buy clomid without insurance

  25. Danieljek表示:

    amoxicillin 875 125 mg tab https://priligymaxpharm.com/# priligy max pharm

  26. StevenNub表示:

    cost of cheap clomid pills: cheap clomid – generic clomid

發佈回覆給「upvc」的留言 取消回覆

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