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,157,883 Responses

  1. Peculiar article, exactly what I needed.

  2. Online Shopping Website In London Tools To Streamline Your Daily Lifethe One Online Shopping Website In London Technique Every Person Needs To Be Able To online shopping website in london

  3. Thanks Joy Organics. cbd gummies sold in texas products tend to be expensive.

  4. Emma表示:

    online slot

  5. What’s The Current Job Market For Uk Women’s Online Shopping Websites Professionals
    Like? uk women’s Online shopping websites

  6. Richard表示:

    lgbt porn

  7. The Most Inspirational Sources Of Window Repair Near Upvc Window Repair Near Me

  8. Pacoima CA表示:

    Keep this going please, great job!

  9. The 10 Most Scariest Things About Uk Online Shopping Sites For
    Electronics uk online shopping sites for electronics

  10. Indira表示:

    What’s The Current Job Market For Window Doctor Near Me Professionals Like?

    window doctor near me (Indira)

  11. To connect with Joy Organics, log into Facebook.

    Feel free to surf to my web-site: royal cbd gummies for sale near me

  12. Bookmarks表示:

    Not everyone wants THC in their CBD products.

    My web page: Bookmarks

  13. I’m amazed, I have to admit. Rarely do I encounter a blog that’s equally
    educative and entertaining, and without a doubt,
    you’ve hit the nail on the head. The issue is something that not enough people are
    speaking intelligently about. I’m very happy that I
    stumbled across this in my hunt for something
    relating to this.

  14. 10 Reasons That People Are Hateful Of Motor Vehicle Law Motor vehicle accident attorney

  15. These products are generally marketed as CBD products.

    Here is my blog … sugar bear sleep gummies ingredients

  16. Bookmarks表示:

    However, very few studies center on CBD and sleep.

    My page; Bookmarks

  17. How To Survive Your Boss On Which Is Best For
    Online Grocery Shopping Kitchen Storage Solution

  18. The amount of organic cbd gummies without thc in each gummy depends on the
    brand.

  19. Bookmarks表示:

    FOCL CBD + CBN Sleep Gummies have quite the résumé.

    Look at my homepage – Bookmarks

  20. Veronique表示:

    The 10 Most Terrifying Things About Online Retailers Uk Stats online
    retailers uk stats (Veronique)

  21. Full-spectrum cbd gummies for sale at cvs contain trace amounts of THC.

  22. Near By表示:

    Windows Repairs Tips From The Top In The Business Near By

  23. I’m not that much of a internet reader to be honest but your sites really nice,
    keep it up! I’ll go ahead and bookmark your website to come back later on. All the best

  24. Joy Organics = 11.75 points. Joy Organics = 21 points.

    Here is my website: organic cbd gummies no thc

發佈留言

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