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,738 Responses

  1. Brandonnot表示:

    canadian pharmacy ed medications: CanadianMdPharm – canadian drugs

  2. kypit 1s_tuor表示:

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

  3. CurtisBam表示:

    canadian pharmacy: Canadian Md Pharm – canadian pharmacy 24h com

  4. kypit 1s_rkor表示:

    1с предприятие купить программу https://svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513/ .

  5. kypit 1s_iwEn表示:

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

  6. CurtisBam表示:

    Mexican Easy Pharm: Mexican Easy Pharm – Mexican Easy Pharm

  7. Medicines information for patients. Cautions.
    metoprolol succ coupon
    Everything trends of medication. Get information here.

  8. AndreDib表示:

    canadian pharmacy ltd https://mexicaneasypharm.shop/# reputable mexican pharmacies online
    top 10 pharmacies in india

  9. Brandonnot表示:

    Indian Cert Pharm: Indian pharmacy international shipping – Best Indian pharmacy

  10. kypit 1s_ewEn表示:

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

  11. It’s great that you are getting ideas from this post
    as well as from our discussion made here.

  12. Jerome Peters表示:

    Thanks for the valuable article. More at online pokies bonuses NZ

  13. kypit 1s_zhEn表示:

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

  14. CurtisBam表示:

    Online pharmacy: Best online Indian pharmacy – Indian Cert Pharm

  15. Brandonnot表示:

    canadian valley pharmacy: 77 canadian pharmacy – 77 canadian pharmacy

  16. kypit 1s_sjEn表示:

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

  17. Brandonnot表示:

    Indian Cert Pharm: Indian Cert Pharm – IndianCertPharm

  18. kypit 1s_ltor表示:

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

  19. AndreDib表示:

    adderall canadian pharmacy https://canadianmdpharm.online/# canadapharmacyonline com
    top online pharmacy india

  20. Brandonnot表示:

    Best Indian pharmacy: Indian Cert Pharm – Online pharmacy

  21. CurtisBam表示:

    reddit canadian pharmacy: online canadian drugstore – canadian pharmacy store

  22. kypit 1s_ffor表示:

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

  23. pendidikan表示:

    I got this web page from my friend who told me on the
    topic of this web site and at the moment this time I
    am browsing this site and reading very informative
    articles at this place.

  24. Brandonnot表示:

    buy drugs from canada: Canadian Md Pharm – best canadian online pharmacy reviews

  25. I found this very interesting. Check out رنگ ضد حریق for more

  26. Can I simply say what a relief to uncover somebody who actually
    understands what they’re talking about on the web.

    You certainly know how to bring a problem to light and make it important.

    More people really need to look at this and understand this side
    of the story. It’s surprising you are not more popular given that you most
    certainly have the gift.

  27. Carrolleruck表示:

    https://mexicaneasypharm.shop/# Mexican Easy Pharm
    canadian pharmacy

發佈留言

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