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,446,892 Responses

  1. See What Accident And Injury Attorneys Tricks The Celebs Are Using accident and Injury attorneys

  2. CurtisBam表示:

    medication canadian pharmacy: CanadianMdPharm – canadapharmacyonline legit

  3. 5 Killer Quora Answers To Double Glazing Doctor glazing doctor

  4. kypit 1s_sbor表示:

    настроить 1с бухгалтерия цена настроить 1с бухгалтерия цена .

  5. Brandonnot表示:

    canadian online pharmacy: Canadian Md Pharm – drugs from canada

  6. Carrolleruck表示:

    https://mexicaneasypharm.com/# mexico drug stores pharmacies
    canada pharmacy online

  7. There are various tools and websites that claim to allow users to view private
    Instagram profiles, but it’s important to right of entry these with caution. Many of these tools can be unreliable, may require personal information, or could violate Instagram’s terms of service.
    Additionally, using such tools can compromise your own security or lead to scams.
    The safest and most ethical exaggeration to view private instagram profiles 2022 a private profile is to send a follow request directly to the user.
    Always prioritize privacy and worship in your online interactions.

  8. AndreDib表示:

    canadian pharmacy king https://canadianmdpharm.shop/# canadian pharmacy 365
    india pharmacy

  9. This is just what I needed; time to address my roof’s moss problem with help from Roof Cleaning Company

  10. kypit 1s_igor表示:

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

  11. kypit 1s_mdEn表示:

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

  12. kypit 1s_avor表示:

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

  13. kypit 1s_wjor表示:

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

  14. AndreDib表示:

    canadian pharmacy ltd https://indiancertpharm.shop/# indian pharmacy
    best online pharmacy india

  15. AndreDib表示:

    buy canadian drugs https://indiancertpharm.shop/# Indian Cert Pharm
    indian pharmacy paypal

  16. bet in asia表示:

    If some one wants to be updated with most recent technologies therefore he must be
    go to see this web site and be up to date daily.

  17. kypit 1s_yhEn表示:

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

  18. Brandonnot表示:

    best canadian online pharmacy: canadian pharmacy 24h com safe – canada drugstore pharmacy rx

  19. I know this if off topic but I’m looking into starting my own blog and
    was wondering what all is needed to get setup?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web smart so I’m not 100% sure.
    Any recommendations or advice would be greatly appreciated.
    Thank you

  20. kypit 1s_gnor表示:

    настройка 1с бухгалтерии цена настройка 1с бухгалтерии цена .

  21. AndreDib表示:

    canadian pharmacy antibiotics https://canadianmdpharm.online/# medication canadian pharmacy
    pharmacy website india

  22. CurtisBam表示:

    Mexican Easy Pharm: buying from online mexican pharmacy – Mexican Easy Pharm

  23. kypit 1s_plEn表示:

    программы фирмы 1с программы фирмы 1с .

  24. 20 Myths About Retro Red Fridge Freezer: Busted Cheapest Retro Fridge Freezer

  25. kypit 1s_bzor表示:

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

發佈留言

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