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,371,120 Responses

  1. Regards, Ample content!

  2. KennethPah表示:

    order prednisone on line prednisone 50 mg price medicine prednisone 5mg

  3. RobertJar表示:

    amoxicillin for sale online: cost of amoxicillin prescription – amoxicillin where to get

  4. Olivia Larson表示:

    This was very well put together. Discover more at live casinos providers

  5. Nikefut表示:

    deep web links darknet links dark markets 2024

  6. KennethPah表示:

    can i buy generic clomid buying cheap clomid without insurance can you buy generic clomid pills

  7. KennethPah表示:

    amoxicillin order online no prescription buy amoxicillin cost of amoxicillin 875 mg

  8. EdgarWat表示:

    Интересные статьи о займах – https://omskpress.ru/news/324878/kak-prosto-vzyat-mikrozajm/

  9. RobertJar表示:

    prednisone 10: Predni Best – generic prednisone cost

  10. DanielAbice表示:

    https://clomidonpharm.com/# where to get generic clomid without dr prescription

  11. I enjoyed this article. Check out 27330 Auto Glass for more

  12. Предлагаем услуги профессиональных инженеров офицальной мастерской.
    Еслли вы искали срочный ремонт стиральных машин smeg, можете посмотреть на сайте: ремонт стиральных машин smeg цены
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  13. DanielAbice表示:

    http://amoxstar.com/# amoxicillin 500 mg capsule

  14. Don Wong表示:

    I had the best time hiking in the nearby trails of Santa Rosa CA. Nature is calling! Discover more hikes at professional marketing solutions Santa Rosa

  15. KennethPah表示:

    prednisone 2.5 mg cost PredniBest prednisone 40mg

  16. Hello, I think your site might be having browser compatibility issues.
    When I look at your blog site in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping.

    I just wanted to give you a quick heads up! Other then that, awesome blog!

  17. RobertJar表示:

    prednisone without prescription medication: prednisone in india – prednisone 10 mg tablet

  18. Pretty nice post. I just stumbled upon your weblog and wished to
    say that I have really enjoyed browsing your
    blog posts. In any case I’ll be subscribing to your rss feed and I hope you write again very soon!

  19. SamuelBix表示:

    amoxicillin 200 mg tablet: AmoxStar – amoxicillin no prescription

  20. SamuelBix表示:

    can you buy cheap clomid: clomidonpharm – buy generic clomid

  21. KennethPah表示:

    where can i buy generic clomid can you buy generic clomid online can i order clomid online

  22. DanielAbice表示:

    https://clomidonpharm.com/# where to buy cheap clomid without rx

  23. Cheers. Great information.

  24. Hey! Do you use Twitter? I’d like to follow you if that would be okay.
    I’m definitely enjoying your blog and look forward to new updates.

    Here is my web page – retirement communities

  25. Lucas Lawson表示:

    Each state has different laws regarding personal injury claims—make sure your lawyer knows them inside best car accident lawyer near me

  26. Lexustogel表示:

    I’ll right away take hold of your rss feed as I can’t to find your e-mail subscription hyperlink or e-newsletter service.

    Do you have any? Kindly let me realize so that I may subscribe.
    Thanks.

發佈留言

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