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,266,104 Responses

  1. Jamesdiure表示:

    пинап казино: пин ап казино – pinup kazi

  2. Ironically, throughout a closing confrontation between Luke Skywalker, Darth Vader and the Emperor aboard a cell battle station known as the Demise Star, the Sith lineage would end as Darth Bane prescribed that it proceed.

  3. You’ll Never Guess This G-Spot Vibrators’s Benefits
    G-spot vibrators

  4. Nikefut表示:

    dark web link black internet bitcoin dark web

  5. 妙に 意味表示:

    Calabria has a lot to offer, and if it’s not as common as different areas, this might be because of the truth that on this space tourism has not been much promoted but.

  6. Medication information leaflet. Cautions.
    street name for buspirone
    Some what you want to know about medication. Get information now.

  7. DarioTob表示:

    пин ап кз: pin up казино – пин ап кз

  8. Jamesdiure表示:

    пин ап казино: пин ап казино – pin up казино

  9. I always spent my half an hour to read this website’s articles daily along with a cup of coffee.

  10. this to say that you may have an erection during sexual arousal.clomid prescription cost uk migraine – most
    of all popular side effect of drug viagra connect.

  11. Leandro表示:

    Yes! Finally something about hacked apps.

    Look into my web-site :: cracked apk mod (Leandro)

  12. In today’s fast-paced world, where dietary excesses and sedentary lifestyles are the norms,
    managing Order RiteLyfe Blood Sugar Stabilizer sugar levels has become a crucial aspect of maintaining
    overall health.

  13. Más aún, descargar dicho contenido y disfrutarlo de forma gratuita en nuestros teléfonos sin tener que pasar por complicados procesos o arriesgar la seguridad del
    mismo.

  14. Jamesdiure表示:

    pinup-kazi.kz: пин ап кз – pin up казино

  15. Hey there! I’ve been reading your blog for some time now and finally got the courage to go ahead and give
    you a shout out from Austin Tx! Just wanted to mention keep up the good work!

  16. Your way of describing everything in this piece of writing is really good, all can easily understand it, Thanks a lot https://www.minientrepotssaintcalixte.ca/7-articles-que-vous-pouvez-stocker-dans-un-entrepot-de-location/

  17. Jamesdiure表示:

    vavada: vavada-kazi.ru – казино вавада

  18. Five Killer Quora Answers On Electric Fire Wall Mounted Electric fire Wall mount

  19. kraken 14at表示:

    My partner and I stumbled over here by a different 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 repeatedly.

  20. HenrydeT表示:

    пин ап казино официальный сайт пин ап казино pinup kazi

  21. nuddies表示:

    Very soon this web page will be famous among all blogging
    users, due to it’s nice content

  22. CurtisIncix表示:

    vavada-kazi.ru: vavada – vavada-kazi.ru

  23. Hmm is anyone else having problems with the images on this blog loading?
    I’m trying to find out if its a problem on my end or if it’s the blog.
    Any responses would be greatly appreciated.

  24. Gibkiy_haPi表示:

    по низкой цене
    плинтус для столешницы самоклеющийся https://elastichnyj-plintus.ru/ .

  25. Essie McGee表示:

    Just got my trees trimmed last week, and I’m amazed at how much light it brought into my yard! Thanks to the team at tree removal for their excellent service

  26. DarioTob表示:

    пинап казино: pin up казино – пинап казино

  27. HenrydeT表示:

    pinup-kazi.ru pinup kazi pinup kazi

發佈留言

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