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

911,254 Responses

  1. You’ll Be Unable To Guess Double Glazed Replacement Windows’s Tricks double glazed replacement windows

  2. JustinCit表示:

    india pharmacy mail order http://indiaph24.store/# п»їlegitimate online pharmacies india
    pharmacy website india

  3. JustinCit表示:

    india pharmacy http://indiaph24.store/# world pharmacy india
    top online pharmacy india

  4. The 10 Most Terrifying Things About Adhd In Older Females Symptoms adhd in Older females symptoms

  5. Guide To Link Alternatif Gotogel: The Intermediate Guide
    Towards Link Alternatif Gotogel Link Alternatif Gotogel

  6. 10 Things Everybody Has To Say About Loveseat For Sale Loveseat For Sale
    couches recliner [https://Articlement.com/]

  7. The 10 Worst Avon Welcome Kit Fails Of All Time Could Have Been Prevented avon welcome kit

  8. Michaelcew表示:

    http://canadaph24.pro/# canadian discount pharmacy

  9. Pedroereva表示:

    mexican drugstore online: Online Pharmacies in Mexico – mexico drug stores pharmacies

  10. Pedroereva表示:

    п»їbest mexican online pharmacies: Online Pharmacies in Mexico – mexican rx online

  11. Marcelabelp表示:

    http://canadaph24.pro/# onlinepharmaciescanada com

  12. Stevenrip表示:

    indian pharmacy online: buy medicines from India – cheapest online pharmacy india

  13. Rickysem表示:

    reputable canadian pharmacy canadian pharmacies recommended canadian pharmacies

  14. Pedroereva表示:

    buying prescription drugs in mexico: cheapest mexico drugs – mexican online pharmacies prescription drugs

  15. Cs go表示:

    What Is All CSGO Skins And Why Is Everyone Speakin’ About It?
    Cs go

  16. Marcelabelp表示:

    https://canadaph24.pro/# trusted canadian pharmacy

  17. JustinCit表示:

    Online medicine order https://indiaph24.store/# indian pharmacy
    indianpharmacy com

  18. What’s The Current Job Market For Upvc Window Handle Replacement Professionals?

    Window Handle Replacement

  19. Rickysem表示:

    mexico pharmacy buying from online mexican pharmacy buying prescription drugs in mexico online

  20. 9 Signs That You’re A Mesothelioma Compensation Expert mesothelioma attorney (http://mspeech.Kr/bbs/board.php?bo_table=705&wr_Id=196429)

  21. Michaelcew表示:

    https://mexicoph24.life/# mexican mail order pharmacies

  22. daftar sv388表示:

    Attractive section of content. I just stumbled upon your
    web site and in accession capital to assert that I acquire in fact
    enjoyed account your blog posts. Anyway I will be subscribing to
    your feeds and even I achievement you access consistently rapidly.

  23. 5 Designer Handbags And Purses Myths You Should Stay Clear
    Of Designer Handbags Names

  24. Rickysem表示:

    buy prescription drugs from india indian pharmacy best india pharmacy

  25. 15 Reasons Not To Ignore Under Desk Treadmill With Incline treadmill for under desk

  26. Five Killer Quora Answers On Replacement Double Glazing Units Near Me replacement double Glazing units near Me

  27. See What Seo Backlink Building Software Tricks The
    Celebs Are Using Seo backlink building software

  28. Michaelcew表示:

    http://canadaph24.pro/# northern pharmacy canada

  29. Marcelabelp表示:

    https://canadaph24.pro/# canada online pharmacy

發佈留言

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