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...

910,720 Responses

  1. Clement表示:

    What Freud Can Teach Us About CSGO Cases To Invest In gloves cases (Clement)

  2. Maryjo表示:

    9 . What Your Parents Teach You About Self Emptying Robot Vacuums self emptying robot vacuums, Maryjo,

  3. How Adding A Window Glass Repair Near Me To Your Life Will Make All The Impact window Repairs

  4. WilliamRhype表示:

    https://ciprofloxacin.tech/# ciprofloxacin mail online

  5. 5 Killer Quora Answers On Rabbit Toy Adult rabbit toy adult (http://www.dermandar.com)

  6. Why You Must Experience Psychiatrist Assessment At Least Once In Your Lifetime
    Psychiatric Assessment Online

  7. Davidtes表示:

    femara vs tamoxifen buy nolvadex online clomid nolvadex

  8. Guide To Double Glazing Repairs Near Me: The Intermediate Guide The Steps To Double Glazing Repairs Near Me double glazing repairs
    near me; http://dnpaint.co.kr/,

  9. 9 . What Your Parents Taught You About Symptoms Of Adhd In Adults Test symptoms of adhd in adults test

  10. These are truly enormous ideas in about blogging. You have touched some nice points here.

    Any way keep up wrinting.

  11. 17 Reasons Why You Should Be Ignoring Branded Slots Top Developer Slots

  12. Davidtes表示:

    where can i buy cipro online ciprofloxacin 500mg buy online cipro pharmacy

  13. 10 Things You Learned In Kindergarden To Help You Get Started With Upvc Window Repairs window repairs near me

  14. Slot Pulsa表示:

    Hi, i think that i saaw youu visited mʏ blog thus i cаme t᧐ “return the favor”.I’m tryiing to find tһings
    to enhance mү website!Ӏ suppolse іts oк to սse a few оf youг
    ideas!!

    Ꭺlso visit my web blog Slot Pulsa

  15. Для игроков Покердом казино действует программа поощрения.

  16. Davidtes表示:

    get cheap propecia tablets cost cheap propecia online cheap propecia online

  17. Local to me表示:

    10 Ways To Create Your ADHD Diagnosis Private
    Empire Local to me

  18. 14 Misconceptions Commonly Held About Private ADHD Test adhd Assessment for adults private

  19. 5 Laws That Can Help The Treating Adhd In Adults Industry Treatment of Adhd in adults

  20. How To Outsmart Your Boss On Local SEO Strategy local seo marketing

  21. What’s The Reason You’re Failing At Key Car Replacement cheap car key replacement

  22. RandallTaulp表示:

    ciprofloxacin: purchase cipro – buy cipro

  23. 17 Reasons To Not Not Ignore Ghost 2 Immobiliser
    Ghost Immobiliser Price

  24. youjizz表示:

    Excellent site you have here.. It’s difficult to find excellent writing like yours nowadays.
    I truly appreciate people like you! Take care!!

  25. Find表示:

    Ten Work For Avon Online-Related Stumbling Blocks
    You Shouldn’t Share On Twitter Find

  26. Window repair表示:

    The History Of Windows Repairs In 10 Milestones
    Window repair

  27. 15 Unquestionable Reasons To Love Truck Accident Attorney Near Me truck Accidents

發佈留言

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