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,671,782 Responses

  1. DevonPhack表示:

    https://mexicanpharm24.cheap/# mexican online pharmacies prescription drugs
    generic ed drugs

  2. What’s The Point Of Nobody Caring About Integrated Fridge
    Freezer With Water Dispenser plumbed fridge freezer with water and ice dispenser

  3. Where Is Best Male Masturbation Toy Be 1 Year From In The Near Future?
    best male masturbation toys

  4. situs 4D表示:

    Guide To Situs 4d: The Intermediate Guide For Situs 4d situs 4D

  5. 5 People You Should Be Getting To Know In The Four Seater Chesterfield Sofa Industry antique chesterfield

  6. Williamagric表示:

    ed treatment drugs http://pharm24.pro/# erectile dysfunction medications

  7. I do not even know how I ended up here, but I thought this post was good.
    I don’t know who you are but definitely you’re going to a famous blogger if you aren’t already ;
    ) Cheers!

    my web-site; https://www.cucumber7.com/

  8. 7 Things About Accident Injury Attorneys Near Me You’ll Kick Yourself For Not Knowing attorney Accident lawyer

  9. LeroyBak表示:

    purple pharmacy mexico price list Mexican pharmacy ship US best online pharmacies in mexico

  10. bunk Bed Uk表示:

    Everything You Need To Learn About Best Bunk Bed bunk Bed Uk

  11. 7 Simple Changes That Will Make A Big Difference In Your Autofold Mobility Scooter automatic folding mobility Scooters

  12. colmek表示:

    Heya i’m for the first time here. I found this board and I find It really
    useful & it helped me out a lot. I hope to give
    something back and help others like you helped me.

  13. Сервисный центр предлагает замена аккумулятора lenovo ideapad s210 touch замена системых охлаждения lenovo ideapad s210 touch

  14. LeroyBak表示:

    indian pharmacies safe Indian pharmacy online reputable indian pharmacies

  15. 20 Trailblazers Setting The Standard In Folding Treadmills treadmills That fold Up

  16. LeroyBak表示:

    mexican drugstore online mexico pharmacy cheap medicine in mexico pharmacies

  17. One Key Trick Everybody Should Know The One Audi A5 Replacement Key
    Trick Every Person Should Know audi replacement key service near
    me – Gogogobookmarks.com,

  18. вывод из запоя цены на дому москва вывод из запоя цены на дому москва .

  19. вывод из запоя в москвае nazalnyj.ru .

  20. Your Worst Nightmare Concerning Espresso Machines Relived coffee espresso machine

  21. Сервисный центр предлагает поменть стекло huawei ascend d1 quad xl замена дисплея huawei ascend d1 quad xl

  22. discuss表示:

    Pretty! Ƭhis was an incredibly wondcerful article.
    Τhanks fօr supplying tһiѕ info.

    Also visit my web site; discuss

  23. Medicines information leaflet. Drug Class.
    can i get generic inderal without insurance
    Best about medication. Get information here.

  24. DevonPhack表示:

    https://mexicanpharm24.cheap/# purple pharmacy mexico price list
    buy ed drugs

  25. değer kaybı talep beyanı

    This is a good tip especially to those fresh to the blogosphere.
    Brief but very precise info… Appreciate your sharing this one.
    A must read post!

發佈回覆給「https://www.cucumber7.com/」的留言 取消回覆

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