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,144,354 Responses

  1. GeorgeSep表示:

    best price for viagra 100mg: Cheap Sildenafil 100mg – Order Viagra 50 mg online

  2. CharlesDog表示:

    https://cialist.pro/# Cialis without a doctor prescription

  3. The 9 Things Your Parents Taught You About Renault Clio Spare
    Key renault clio Spare Key

  4. ElmerWes表示:

    cenforce.pro Buy Cenforce 100mg Online Cenforce 150 mg online

  5. ElmerWes表示:

    cenforce.pro buy cenforce Cenforce 100mg tablets for sale

  6. What Is Erb’s Palsy Case And Why Is Everyone Talking About It?

    indianola erb’s palsy attorney

  7. ElmerWes表示:

    Cenforce 100mg tablets for sale Buy Cenforce 100mg Online Purchase Cenforce Online

  8. hi!,I love your writing very so much! percentage we communicate more about your article on AOL?
    I need an expert on this space to resolve my problem.
    Maybe that is you! Looking forward to see you.

    Feel free to surf to my web blog เกร็ดความรู้

  9. DavidOwelo表示:

    http://cenforce.pro/# cheapest cenforce

  10. 10 Facts About Locksmith Cars That Can Instantly Put You In A Good Mood local car locksmith near me

  11. The Reasons 9kg Washing Machines Sale Is Tougher
    Than You Think 9kg washing Machines sale

  12. See What Double Glazing Leeds Tricks The Celebs Are Making Use Of Double Glazing Leeds

  13. 24 Hours To Improving Upvc Window Repairs Window Repairs Near Me

  14. GeorgeSep表示:

    Levitra 20 mg for sale: Cheap Levitra online – Levitra 20 mg for sale

  15. TylerPhona表示:

    http://viagras.online/# Cheap Viagra 100mg

  16. 10 Things We All Hate About Replacement Bmw Keys genuine bmw Replacement key uk

  17. JamesNow表示:

    cheap viagra: Buy Viagra online cheap – Cheap generic Viagra online

  18. Buzzwords De-Buzzed: 10 Other Methods To Deliver Bean To Cup Coffee Machine Bean To Cup Machines

  19. I’ve been exploring for a bit for any high-quality articles
    or blog posts in this sort of house . Exploring in Yahoo I eventually stumbled upon this web site.
    Reading this info So i’m satisfied to express that I’ve an incredibly good uncanny
    feeling I came upon exactly what I needed. I so much undoubtedly will make sure to do not put out of your mind this website and give it
    a glance on a continuing basis.

  20. ElmerWes表示:

    Viagra Tablet price Cheap Viagra 100mg Viagra online price

  21. 15 Best Pinterest Boards Of All Time About Tommy Hilfiger Crossbody Handbags
    Tommy Hilfiger Cross Body Bag

  22. CharlesDog表示:

    https://viagras.online/# Generic Viagra online

  23. Movies表示:

    Hi there! This post could not be written any better! Reading
    through this post reminds me of my old room mate! He always kept talking about this.

    I will forward this write-up to him. Fairly certain he
    will have a good read. Many thanks for sharing!

  24. CharlesDog表示:

    http://cenforce.pro/# buy cenforce

  25. vimeo.com表示:

    13 Things You Should Know About Erb’s Palsy Lawsuit That You Might Not Have
    Considered vimeo.com

  26. Why Pleural Mesothelioma Compensation Will
    Be Your Next Big Obsession Mesothelioma compensation claims

  27. JamesNow表示:

    Kamagra 100mg price: buy kamagra online – Kamagra Oral Jelly

  28. Velma表示:

    The Largest Issue That Comes With Replacement Windows Leeds, And How
    You Can Fix It french door repairs leeds [Velma]

  29. CharlesDog表示:

    https://viagras.online/# buy viagra here

發佈留言

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