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,450,729 Responses

  1. CurtisBam表示:

    pharmacies in mexico that ship to usa: best online pharmacies in mexico – purple pharmacy mexico price list

  2. RileyBok表示:

    Viagra * Cialis * Levitra

    All the products you are looking for are currently available in support of 1+1.

    4 more tablets of identical of the following services: Viagra * Cialis * Levitra

    https://xn--2i0bm4p0sf2wh7vdmsy.net

  3. Carrolleruck表示:

    https://indiancertpharm.shop/# Indian Cert Pharm
    canada drugstore pharmacy rx

  4. CurtisBam表示:

    Indian pharmacy that ships to usa: Online pharmacy – Indian pharmacy that ships to usa

  5. Appreciate the detailed information. For more, visit عمل پی ار کی

  6. I was extremely pleased to uncover this page. I need to to thank you for your time for this particularly
    wonderful read!! I definitely loved every little bit
    of it and i also have you book-marked to see new information on your blog.

  7. I am sure this post has touched all the internet viewers, its really really nice piece of writing on building
    up new website.

  8. kypit 1s_jhEn表示:

    1с предприятие купить программу 1с предприятие купить программу .

  9. CurtisBam表示:

    buying from online mexican pharmacy: Mexican Easy Pharm – mexican border pharmacies shipping to usa

  10. Alex Manning表示:

    This post inspired me to think outside the box with my own backyard space; thank you for the motivation—more inspiration awaits at deck builder in charlotte

  11. WilliamBak表示:

    Хотите купить окна melke pro по разумной цене? Ознакомьтесь с нашим предложением! У нас — качество, надежность и стиль по доступной стоимости. Индивидуальный подход к каждому заказу!

  12. Hi, I do think this is a great site. I stumbledupon it 😉 I may return yet again since I saved
    as a favorite it. Money and freedom is the best way to change, may
    you be rich and continue to help other people. http://balikesir.ogo.org.tr/question/snowboarding-gear-in-canada-essential-equipment-for-the-ultimate-ride-4/

  13. menwiki.men表示:

    The Secret Secrets Of Medium Scooters lightweight electric
    mobility foldable scooter, menwiki.men,

  14. CurtisBam表示:

    Indian Cert Pharm: Indian Cert Pharm – Indian Cert Pharm

  15. CurtisBam表示:

    Online medicine: indian pharmacy – indian pharmacy

  16. The quality of tiles from Abbey Carpet & Floor at Patricia’s is unmatched in Cape Coral! Love my new floors! tile retailer

  17. CurtisBam表示:

    legit canadian pharmacy online: CanadianMdPharm – canadian family pharmacy

  18. Brandonnot表示:

    mexican rx online: Mexican Easy Pharm – Mexican Easy Pharm

  19. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each
    time a comment is added I get several e-mails with the same comment.
    Is there any way you can remove people from that service?
    Appreciate it! http://anyang.xn--2o2b15m1Xf36o.com/bbs/board.php?bo_table=problem&wr_id=1875393

  20. PENIPU表示:

    Does your website have a contact page? I’m having a
    tough time locating it but, I’d like to send you an email. I’ve got some recommendations for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it improve over time.

  21. Kindra表示:

    The Top Iphone Apps – What’s The Secret Associated With Their Success?
    다바오충전; Kindra,

  22. Isabella表示:

    5 Must-Know Nissan Micra Replacement Key-Practices You
    Need To Know For 2024 replacement nissan keys, Isabella,

  23. CurtisBam表示:

    Best online Indian pharmacy: Indian pharmacy that ships to usa – Indian Cert Pharm

  24. kypit 1s_rqor表示:

    программа 1с купить с установкой программа 1с купить с установкой .

發佈留言

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