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,478,382 Responses

  1. Узнай все о удлинение полового члена москва увеличение длины полового члена

  2. Emily Briggs表示:

    Great reminders about the impact of weather on roofs—I’ll be scheduling regular inspections from now on! roofing contractors vancouver

  3. excellent issues altogether, you just received a new
    reader. What would you recommend about your publish that
    you made a few days in the past? Any sure?

  4. BernardMop表示:

    what is minocycline prescribed for buy Stromectol minocycline 50mg without a doctor

  5. AlbertDuM表示:

    cytotec pills buy online: cyt premium – cytotec abortion pill

  6. BernardMop表示:

    plavix best price PlavixClo Best Price buy clopidogrel online

  7. You actually said it terrifically.

  8. I got this web page from my friend who told me concerning this web site and at the moment this time I am visiting this web site and
    reading very informative articles or reviews at this time.

  9. BrandenSlomy表示:

    amoxicillin where to get: com pharm – how to buy amoxycillin

  10. Ada Cummings表示:

    For the best pressure cleaning service in Conway, I always advise house cleaning services conway ar

  11. wps office表示:

    I am genuinely grateful to the owner of this website who has shared this fantastic
    piece of writing at at this place.

  12. Kristopher表示:

    Are You Getting The Most Value Of Your Best Male Masturbators?
    best male mastrabator (Kristopher)

  13. It’s hard to come by well-informed people on this
    subject, however, you seem like you know what you’re talking
    about! Thanks

    my website – คาสิโนauto

  14. BrandenSlomy表示:

    where to buy prednisone in australia: ray pharm – 50mg prednisone tablet

  15. Mae Bowman表示:

    Fantastic advice on using video content for law firms! It’s a great way to improve SEO and engage potential clients seo for legal firm

  16. Узнай все о хирургическое увеличение члена операция по увеличению члена

  17. cuaca778表示:

    If you desire to get a good deal from this post
    then you have to apply such strategies to your won website.

  18. BrandenSlomy表示:

    cost generic clomid for sale: clomid – get generic clomid pills

  19. AlbertDuM表示:

    Plavix 75 mg price: Clopidogrel Best Prices – buy Clopidogrel over the counter

  20. Excellent blog post. I certainly love this site.

    Stick with it!

  21. DerekWab表示:

    кухни на заказ – сочетание оригинального дизайна и практичности.

  22. Узнай все о удлинение полового члена цена увеличить член цена

  23. id=”firstHeading” class=”firstHeading mw-first-heading”>Search rеsults

    Ꮋelp

    English

    Tools

    Tools
    mоᴠe to sidebar hide

    Actons

    Ԍeneral

    Aⅼѕo visit mү web-site พวงหรีด งานศพ

  24. AlbertDuM表示:

    buy Lisinopril 1st: cheapest Lisinopril – buy Lisinopril online

發佈留言

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