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,196,215 Responses

  1. Henrylip表示:

    mexican rx online: cmq mexican pharmacy online – mexico drug stores pharmacies

  2. I have been surfing online more than 4 hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. In my opinion, if all web owners and
    bloggers made good content as you did, the net will be a
    lot more useful than ever before.

    Here is my web page asbestos removal ottawa

  3. MichaelPayof表示:

    п»їbest mexican online pharmacies
    http://cmqpharma.com/# mexican rx online
    mexican border pharmacies shipping to usa

  4. Henrylip表示:

    mexico drug stores pharmacies: cmq pharma mexican pharmacy – mexican rx online

  5. Hello outstanding website! Does running a blog such as this require a
    lot of work? I’ve virtually no understanding of coding however I had been hoping to start my own blog in the
    near future. Anyway, if you have any recommendations
    or techniques for new blog owners please share. I understand this is off
    subject however I just needed to ask. Appreciate it!

    Have a look at my site :: copier service austin

  6. Henrylip表示:

    mexican pharmacy: cmq pharma – best online pharmacies in mexico

  7. Briantes表示:

    Добрый день!
    Заказать диплом ВУЗа.
    Наш сервис предлагает приобрести диплом в высоком качестве, неотличимый от оригинала без участия специалистов высокой квалификации со специальным оборудованием.
    Где заказать диплом по актуальной специальности?
    http://mdr7.ru/viewtopic.php?f=6&t=8707
    Хорошей учебы!

  8. Jayson表示:

    The People Closest To Mesothelioma Asbestos
    Uncover Big Secrets Jayson

  9. jerealas表示:

    Double Glazing Window Locks Repairs: It’s
    Not As Difficult As You Think jerealas

  10. Henrylip表示:

    purple pharmacy mexico price list: cmqpharma.com – purple pharmacy mexico price list

  11. Henrylip表示:

    medication from mexico pharmacy: cmq pharma – п»їbest mexican online pharmacies

  12. Henrylip表示:

    purple pharmacy mexico price list: buying from online mexican pharmacy – purple pharmacy mexico price list

  13. Henrylip表示:

    medication from mexico pharmacy: cmq mexican pharmacy online – reputable mexican pharmacies online

  14. Pretty portion of content. I just stumbled upon your web site and in accession capital to claim that I acquire in fact loved account your weblog
    posts. Any way I will be subscribing for your augment and even I fulfillment you get admission to constantly quickly.

  15. jerealas表示:

    10 Of The Top Mobile Apps To Double Glazing Repair Kit jerealas

  16. Henrylip表示:

    п»їbest mexican online pharmacies: mexican online pharmacy – reputable mexican pharmacies online

  17. MichaelPayof表示:

    medicine in mexico pharmacies
    https://cmqpharma.com/# mexico pharmacies prescription drugs
    mexico drug stores pharmacies

  18. Приветствую. Может кто знает, где найти полезные блоги о недвижимости? Сейчас читаю https://teplohod-denisdavidov.ru

  19. Henrylip表示:

    buying from online mexican pharmacy: mexico pharmacy – mexico pharmacies prescription drugs

  20. MichaelPayof表示:

    buying from online mexican pharmacy
    http://cmqpharma.com/# buying from online mexican pharmacy
    п»їbest mexican online pharmacies

  21. If some one wants expert view regarding running a blog then i advise him/her to go to see this blog,
    Keep up the fastidious work.

  22. MichaelPayof表示:

    mexico pharmacies prescription drugs
    http://cmqpharma.com/# medicine in mexico pharmacies
    mexican online pharmacies prescription drugs

  23. MichaelPayof表示:

    mexico pharmacies prescription drugs
    http://cmqpharma.com/# mexican pharmaceuticals online
    mexican rx online

發佈留言

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