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,126,758 Responses

  1. 101 game表示:

    Hello! I simply wish to give you a huge thumbs up for your great information you have right
    here on this post. I am coming back to your site for more soon.

  2. Find out how表示:

    I just like the valuable info you supply on your articles.
    I will bookmark your weblog and check again right here frequently.
    I’m rather sure I will be told plenty of new stuff right here!

    Good luck for the following!

  3. nerve pro 6表示:

    Fantastic site. Lots of helpful info here. I am sending it to some buddies ans also sharing in delicious.
    And naturally, thanks in your sweat!

    My web blog … nerve pro 6

  4. 20 Resources To Make You More Efficient At 3 Wheel Rollators premium quality rollator

  5. 20 Trailblazers Setting The Standard In Bunk Beds For Kids Bunk beds for kids sale

  6. Paito Taiwan表示:

    It’s enormous that you are getting ideas from this piece of writing
    as well as from our argument made at this time.

  7. Easydrorbix表示:

    is world pharmacy store legit: legal online pharmacy percocet – uk pharmacy nolvadex

  8. Medicines information. Brand names.
    can i buy cheap coumadin price
    Best information about drugs. Read information now.

  9. Drstobeese表示:

    ketoconazole shampoo pharmacy: wegmans pharmacy online – viagra pharmacy uk

  10. Private ADHD Assessment Cost: It’s Not As Difficult As You Think
    private Adhd assessment west Yorkshire (http://www.stes.tyc.edu.tw)

  11. Drstobeese表示:

    best online pharmacy buy accutane: legitimate online pharmacy – medicine store pharmacy

  12. Good post. I learn something totally new and challenging on sites I stumbleupon every day.
    It will always be helpful to read content from other writers and
    use a little something from their websites.

  13. снятие ломок на дому snyatie-lomki-narkolog11.ru .

  14. Easydrorbix表示:

    pantoprazole pharmacy: overseas online pharmacy no prescription – 24 hr pharmacy near me

  15. Drstobeese表示:

    levitra coupons pharmacy: rx mart pharmacy – cialis in indian pharmacy

  16. The Most Inspirational Sources Of Private Psychiatrists Near Me private psychiatrist Prices

  17. リアルドールを購入する前に、萌々生き生きとしたアニメドール美少女から本物の人間のようなリアルドール女神まで、ラブドール エロ等身大ドールの豊富なコレクションをご覧ください。

  18. Если вы искали где отремонтировать сломаную технику, обратите внимание – ремонт бытовой техники

  19. Easydrorbix表示:

    winn dixie pharmacy: online pharmacy viagra australia – cialis online pharmacy us

  20. 10 Upvc Door Hinges Replacement That Are Unexpected Upvc Windows Hinges (https://Ai-Db.Science)

  21. ugzhnkchr.ru表示:

    7 Things You’ve Never Known About Lost Lexus Key spare lexus key Fob (ugzhnkchr.ru)

  22. 82 bet login表示:

    Terrific work! This is the kind of info that are
    meant to be shared across the internet. Disgrace on the search engines for no longer positioning this publish higher!
    Come on over and visit my website . Thank you =)

  23. It’s The Next Big Thing In Programming Car Key
    Programming Car keys near me

  24. RTP Дизайн человека https://raschet-karty-dizayn-cheloveka.ru Дизайн человека. 4/1 Дизайн человека.

  25. ytmp3s.nu表示:

    Wow, that’s what I was searching for, what a stuff!
    existing here at this web site, thanks admin of this website.

發佈留言

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