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,467,006 Responses

  1. Louis Carroll表示:

    Chiropractic treatments have made such a difference in my life! Excited to see what Lakewood Chiropractor offers in Lakewood

  2. BradleyEdino表示:

    deneme bonusu veren siteler yat?r?ms?z deneme bonusu veren siteler deneme bonusu veren siteler

  3. Augustina表示:

    Incredible! This blog looks exactly like my old one!

    It’s on a entirely different subject but it has pretty much the
    same layout and design. Superb choice of colors!

  4. It’s an amazing paragraph for all the internet visitors; they will obtain advantage from it I am sure.

  5. Vania表示:

    Signature Drinks 오피사이트 – Vania

  6. 10 Inspirational Graphics About Private ADHD Clinic Private Adhd Assessment Coventry

  7. BradleyEdino表示:

    casino bahis siteleri Canl? Casino Siteleri deneme bonusu veren casino siteleri

  8. Donny表示:

    Useful information. Fortunate me I discovered
    your site unintentionally, and I’m shocked why this accident did not came about in advance!
    I bookmarked it.

  9. Meds information sheet. Cautions.
    where to buy cheap trazodone price
    All about medicament. Read now.

  10. Wallacefex表示:

    slot siteleri: en kazancl? slot oyunlar? – slot oyunlar?

  11. Rolando表示:

    Hi there, You have done an excellent job. I’ll certainly digg it and personally suggest to my
    friends. I’m sure they will be benefited from this web site.

  12. Trade Avapro表示:

    I’m truly enjoying the design and layout of your
    blog. It’s a very easy on the eyes which makes it much more pleasant for me
    to come here and visit more often. Did you hire out a designer to create your theme?

    Great work!

  13. 5 Killer Quora Answers On Best Car Locksmith Near Bedfordshire
    Car locksmith near Bedfordshire

  14. 전남호빠表示:

    전남쪽은 저희 전남호빠가 꽉 잡고 있습니다.

  15. FrankEmine表示:

    guvenilir casino siteleri: en guvenilir casino siteleri – canl? casino siteleri

  16. Wallacefex表示:

    slot oyunlar? puf noktalar?: slot siteleri – slot oyunlar? puf noktalar?

  17. Katie Thomas表示:

    This was a wonderful guide. Check out hier überprüfen for more

  18. http://www.airlines-inform.ru/personal/user/?UID=73969 [url=www.www.liveinternet.ru/users/promokodherb/profile]www.www.liveinternet.ru/users/promokodherb/profile[/url] .

  19. Sean Ramirez表示:

    Skup nieruchomości to idealna opcja dla tych, którzy potrzebują natychmiastowej gotówki za swoją nieruchomość. Dzięki temu procesowi można uniknąć długotrwałych formalności związanych z tradycyjną sprzedażą skup mieszkań Warszawa

  20. Wallacefex表示:

    deneme bonusu veren siteler: deneme bonusu veren siteler yeni – deneme bonusu veren siteler

  21. I like the helpful information you supply for your articles.
    I’ll bookmark your weblog and take a look at once more right here frequently.
    I am relatively sure I will be informed plenty of new stuff proper
    right here! Good luck for the next!

  22. FrankEmine表示:

    slot siteleri: en cok kazand?ran slot oyunlar? – slot casino siteleri

  23. Common Poker Mistakes Newer Poker Players Often Unwittingly Make 에볼루션 티비

  24. É maravilhoso ver como a comunidade brasileira se junta em Paris! Histórias inspiradoras e muitas dicas legais por aí, não posso perder o guia brasileiro na frança

  25. 오피表示:

    Adult Entertainment 오피

  26. Viola表示:

    What’s The Job Market For Repairs To Double Glazing Windows Professionals?
    repairs to double glazing windows, Viola,

發佈留言

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