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,238,894 Responses

  1. DevonPhack表示:

    http://pharm24.pro/# new ed drugs
    medication for ed dysfunction

  2. Mesothelioma lawyers specialize in asbestos litigation,
    which includes trust fund claims and lawsuits.
    To be able to successfully handle these claims, they need to be familiar with
    complex state laws and the national legal requirements.

  3. rust cheats表示:

    Quality articles is the key to be a focus for
    the visitors to go to see the web site, that’s what this web page is providing.

  4. DevonPhack表示:

    http://indianpharm24.pro/# best india pharmacy
    buy prescription drugs from india

  5. Williamagric表示:

    best ed pills online http://indianpharm24.pro/# indian pharmacy

  6. Jorgeagopy表示:

    reputable indian online pharmacy: Order medicine from India to USA – indian pharmacies safe

  7. Jorgeagopy表示:

    online shopping pharmacy india: Pharmacies in India that ship to USA – п»їlegitimate online pharmacies india

  8. بت زنده表示:

    What a material of un-ambiguity and preserveness of precious familiarity concerning unpredicted emotions.

  9. Williamagric表示:

    prescription drugs without prior prescription https://pharm24.pro/# buy canadian drugs

  10. Williamagric表示:

    herbal ed treatment https://mexicanpharm24.cheap/# mexico drug stores pharmacies

  11. Jorgeagopy表示:

    buy prescription drugs from india: indian pharmacy purchase online – online pharmacy india

  12. LeroyBak表示:

    ed drugs cheaper medications prescription without a doctor’s prescription

  13. BrandonSaisy表示:

    reputable mexican pharmacies online: mexican pharm 24 – purple pharmacy mexico price list

  14. Data Virdsam表示:

    Hi my friend! I wish to say that this post is amazing, nice written and include almost all vital infos.
    I would like to see extra posts like this .

    https://datalaos1.ambarita.org/

  15. LeroyBak表示:

    mexico drug stores pharmacies mexican pharmacy mexico drug stores pharmacies

  16. BrandonSaisy表示:

    the best ed pill: low cost pharmacy – buy erection pills

  17. Medication information leaflet. What side effects?
    how to get generic fexofenadine price
    All trends of meds. Get now.

  18. The Main Issue With ADHD Private Diagnosis, And How You Can Repair It Private Adhd Assessment Exeter

  19. My spouse and I stumbled over here coming from
    a different web page and thought I might check things out.
    I like what I see so i am just following
    you. Look forward to looking at your web page for a second time.

    https://w3.sydneypoolsday.cc/

  20. Aromatherapy表示:

    This site definitely has all of the information I needed about this subject and didn’t know
    who to ask.

  21. Its like you read my mind! You appear to know a lot about this,
    like you wrote the book in it or something. I think that you can do with a few
    pics to drive the message home a bit, but instead of that, this is
    excellent blog. A great read. I’ll certainly be back.

  22. The 10 Most Scariest Things About Situs Toto Login situs toto login

  23. Key for Bmw表示:

    Key For Bmw Tools To Help You Manage Your Daily Life Key For Bmw
    Trick That Every Person Must Learn Key for Bmw

發佈留言

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