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,538,275 Responses

  1. Wallacefex表示:

    guvenilir slot siteleri: slot oyunlar? puf noktalar? – az parayla cok kazandiran slot oyunlar?

  2. Hallie Massey表示:

    This was a great help. Check out استخدام نیروی نمایشگاهی for more

  3. BradleyEdino表示:

    Casino Siteleri Casino Siteleri deneme bonusu veren casino siteleri

  4. It’s a pity you don’t have a donate button! I’d without a doubt donate to this superb blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this website with my Facebook group. Talk soon!

  5. BradleyEdino表示:

    slot casino siteleri guvenilir slot siteleri slot casino siteleri

  6. Wallacefex表示:

    en guvenilir casino siteleri: en guvenilir casino siteleri – Casino Siteleri

  7. Please let me know if you’re looking for a article writer
    for your blog. You have some really good posts and I think I
    would be a good asset. If you ever want to take some of the load off, I’d
    absolutely love to write some articles for your blog in exchange
    for a link back to mine. Please shoot me an e-mail if interested.
    Thank you!

  8. Cleta表示:

    Way cool! Some very valid points! I appreciate
    you penning this post plus the rest of the website is extremely good.

  9. Having read this I thought it was extremely informative.
    I appreciate you taking the time and energy to put
    this content together. I once again find myself spending a lot of time both reading
    and commenting. But so what, it was still worthwhile!

  10. Virginia表示:

    I think the admin of this website is really working hard for his web site, since here every stuff
    is quality based data.

  11. It’s іn fact very compⅼicated in this full of activity life to listen news on Television, so I just use world wide
    ᴡeb for tһat reason, and take the latest news.

    Here is my bl᧐g post … adderall online usa

  12. OP表示:

    Strip Club OP

  13. Wallacefex表示:

    sweet bonanza giris: sweet bonanza slot – sweet bonanza oyna

  14. The 10 Scariest Things About Patio Doors Repairs Near Me patio doors Repairs near me

  15. Wilfred表示:

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

  16. Appreciate the great suggestions. For more, visit رنگ ضد حریق

  17. Hi there! I could have sworn I’ve been to this website before but after browsing through some of the post
    I realized it’s new to me. Anyhow, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  18. The Best Guide To Baccarat Gambling Rules 에볼루션 y

  19. LlprPaish表示:

    to get what you needOnline comparison shopping allows you to п»їzantac locally, why should I have to shop online?

  20. Shantae表示:

    Awesome things here. I’m very happy to see your article.

    Thanks so much and I am looking ahead to contact you. Will you
    kindly drop me a mail?

  21. Wallacefex表示:

    guvenilir slot siteleri: guvenilir slot siteleri – guvenilir slot siteleri

  22. It’s actually a nice and useful piece of info. I’m happy
    that you just shared this helpful information with us.
    Please stay us up to date like this. Thank you
    for sharing.

  23. Simone表示:

    My brother suggested I would possibly like this blog. He was totally
    right. This submit actually made my day. You cann’t consider simply how a lot time I had
    spent for this info! Thank you!

  24. JasonUnsoG表示:

    https://casinositeleri25.com/# canl? casino siteleri

發佈留言

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