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,478,472 Responses

  1. Barrybib表示:

    best price for viagra 100mg: FastPillsForMen – viagra canada

  2. Hvac Seattle表示:

    What is New U Life SOMADERM Transdermal Gel.

    Also visit my site; Hvac Seattle

  3. Allow me show you in my review of NewULife.

    Review my web page – hvac service seattle wa

  4. An artificial USP human growth hormone (somatropin).

    My website – Hvac contractors Seattle

  5. hvac seattle表示:

    What is New U Life SOMADERM Transdermal Gel.

    My web-site: hvac seattle

  6. jav khong che表示:

    great issues altogether, you simply won a logo new reader.
    What could you suggest in regards to your publish that you just made some days in the past?
    Any positive?

  7. In addition to understanding the principles of their own HOA, homeowners in Jennings, Louisiana also needs to be aware of state legal guidelines governing insurance protection in order to ensure they’re adequately coated.

  8. Why You’re Failing At Asbestosis Asbestos Mesothelioma Attorney Mesothelioma Lawsuit

  9. This blog post is super helpful! Roof replacement seems less intimidating now. For expert advice, don’t forget to visit new roof installation

  10. This was quite informative. For more, visit copiers near albany, ny

  11. 紙飛機 app表示:

    Telegram 係一款流行嘅即時通訊應用,紙飛機官網提供中文版本,用戶可以点击下載 紙飛機 。呢個平台唔單止有電腦桌面版,仲提供適用於蘋果手機同安卓設備嘅 APK 版本。

  12. 부산表示:

    Bangkok Guest Friendly Hotels – Where To Shop And Stay 부산

  13. Barrybib表示:

    cheap erection pills: best online ed pills – cheapest ed pills

  14. Melvin Hart表示:

    Appreciate the thorough write-up. Find more at Klicken Sie hier für die Quelle

  15. Spot on with this write-up, I truly think this web site needs much more attention. I’ll
    probably be back again to read through more, thanks for the info!

  16. My spouse and I stumbled over here different page and thought I should check things out.

    I like what I see so i am just following you.

    Look forward to looking at your web page for a second time.

  17. Clara Ortega表示:

    Flowers can evoke such strong memories and feelings, don’t you assume? They have a means of attaching us to minutes in time. Share your ideas with me at buy roses online

  18. Wonderful beat ! I wish to apprentice even as you amend your website, how can i subscribe for a blog web site?

    The account helped me a acceptable deal. I have been tiny bit
    acquainted of this your broadcast offered vivid transparent idea

  19. BrettTIT表示:

    cheapest viagra: Fast Pills For Men – buy viagra here

  20. срочное оформление загранпаспорта срочное оформление загранпаспорта .

  21. Luke Morton表示:

    A simple change like adding screens can make your outdoor area feel so much cozier—head over to Pool screen repair for

  22. Charlesbot表示:

    https://fastpillsformen.com/# Cheap Sildenafil 100mg

  23. 알밤表示:

    Tis The Time Of Year To Prosper: Smart Holiday Marketing To Ramp Up Your Revenues 알밤

  24. Lettie Wagner表示:

    Nothing beats a sundown cruise in Austin! I located an distinctive boat apartment provider at banana boat

  25. xnxx jordi表示:

    Nicely put, Thank you!

  26. Fkwiki.Win表示:

    10 Things We All We Hate About Boarding Up Door
    Boarding Up Windows And Doors (Fkwiki.Win)

發佈留言

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