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,247,652 Responses

  1. Dereketerm表示:

    where can i buy generic clomid without a prescription: generic clomid – how to buy cheap clomid now

  2. Iyannaobesk表示:

    игровой аппарат fortune mouse gratis
    fortune mouse gratis на деньги

  3. AngieTon表示:

    Не могу сейчас принять участие в дискуссии – нет свободного времени. Очень скоро обязательно выскажу своё мнение.
    One of the largest gaming floors in-d?n h?zz al?n mosbet Oklahoma. play our action-packed promotions, with a players club that rewards of most passionate gamers.

  4. I love your blog.. very nice colors & theme.

    Did you make this website yourself or did you hire someone to do it for you?

    Plz respond as I’m looking to construct my
    own blog and would like to know where u got this from. thanks a lot

  5. BrandenSlomy表示:

    can you get generic clomid tablets: can you get cheap clomid without prescription – buy generic clomid prices

  6. JacobLoody表示:

    Кухни на заказ — узнайте, как создать идеальный дизайн кухни для вашего дома

  7. I do not even know how I ended up here, but I thought
    this post was good. I don’t know who you are but certainly you’re going to a famous blogger
    if you are not already 😉 Cheers!

  8. Nikefut表示:

    dark markets 2024 deep web drug store https://darknetmarketstore.com/ – best darknet markets

  9. BrandenSlomy表示:

    generic prednisone pills: prednisoneraypharm – where to buy prednisone uk

  10. Dereketerm表示:

    priligy max pharm: Priligy tablets – cheap priligy

  11. LeonardKnism表示:

    кухни на заказ в спб – Идеальное сочетание стиля и качества для вашей кухни в Санкт-Петербурге.

  12. Medicines information. Long-Term Effects.
    can you buy generic cialis soft prices
    Best information about drugs. Get information now.

  13. Travis表示:

    What’s Going down i am new to this, I stumbled upon this I have discovered It absolutely helpful and
    it has aided me out loads. I’m hoping to contribute & assist other users
    like its aided me. Great job.

  14. Nikefut表示:

    darknet sites dark web market list https://darknetmarketstore.com/ – free dark web

  15. Use uma selecao conveniente de respostas de nossos especialistas.
    Porque e que sonho em apanhar damascos?

    [url=http://bh-prince2.sakura.ne.jp/cgi-bin/bbs/reportbbs/yybbs.cgi?list=thread]a]Sonha com um cao qu[/url] [url=https://liftoff2lewis.com/guestbook-2/#comment-6683]Porque e que sonho com uma cruz partida?[/url] [url=http://forum.altaycoins.com/viewtopic.php?pid=1955352#p1955352]Sonho de um gato adormecido[/url] [url=https://community.katawatbusiness.com/question/porque-e-que-sonho-com-o-kvass-4/]Porque e que sonho com o kvass?[/url] [url=https://aaronhigley.com/2019/01/23/professional-graphic-designer/#comment-21663]Sonho de loja de penhores[/url] 9793983

發佈留言

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