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,466,932 Responses

  1. BradleyEdino表示:

    deneme bonusu veren casino siteleri guvenilir casino siteleri en guvenilir casino siteleri

  2. Jason Wheeler表示:

    Thanks for the great explanation. More info at استخدام نیروی نمایشگاهی

  3. Thanks for sharing this info about roof maintenance—so crucial for homeowners in Bellingham Roof Cleaning Service

  4. Wallacefex表示:

    slot siteleri: guvenilir slot siteleri – slot casino siteleri

  5. Hurrah, that’s what I was exploring for, what a material!

    present here at this web site, thanks admin of this site.

  6. Owen Boyd表示:

    Thanks for the helpful advice. Discover more at رنگ ضد حریق

  7. BradleyEdino表示:

    sweet bonanza sweet bonanza kazanma saatleri sweet bonanza kazanma saatleri

  8. OP表示:

    Components For Building Amazing Chiropractic Business Empire OP

  9. вывод из запоя стационар ростов вывод из запоя стационар ростов .

  10. Love the focus on sustainable plumbing solutions in your articles—keep it up! 24/7 Plumber

  11. Douglas Young表示:

    I lately started a blossom yard, and it’s been so fulfilling! The delight of nurturing plants is something every person must experience. Discover suggestions on horticulture at flower shops san francisco

  12. I was fretted about security when leasing a bounce house, yet the firm I made use of from water slide rentals near me had impressive testimonials and excellent tools

  13. tsfuc表示:

    提供最新搜狗输入法下载服务,适用于Windows、Mac、Android和iOS设备。支持拼音、五笔、语音输入多种输入方式,词库丰富,输入流畅精准。更有智能联想、个性化皮肤和表情推荐功能,满足多场景输入需求。立即下载搜狗输入法,畅享高效、便捷、个性化的输入体验!https://www.sogoushurufa.org/         xoajmjde

  14. Most modern analysts permit that will certainly is a lot more usual in almost
    all uses.

  15. капельница от запоя ростов на дону капельница от запоя ростов на дону .

  16. BradleyEdino表示:

    sweet bonanza slot sweet bonanza demo oyna sweet bonanza

  17. ” A lot of over the counter therapies will certainly have a concentration under 16 percent peroxide,” she discusses.

  18. вывод из запоя в стационаре ростова http://www.vyvod-iz-zapoya-rostov27.ru .

  19. By seeing a skin specialist, you’ll discover what’s going on which can bring assurance.

  20. BradleyEdino表示:

    slot casino siteleri slot oyunlar? slot oyunlar?

  21. What’s The Current Job Market For Evolution Baccarat Experience Professionals Like?
    에볼루션 룰렛

  22. Otilia表示:

    Furthermore, it can provide information regarding the products made use of
    in creating these improvements.

  23. I every time spent my half an hour to read this webpage’s content all the time
    along with a cup of coffee.

    Also visit my webpage: oud aceite esencial

  24. This action can differ relying on the certain peptide and the receptor it binds to.

  25. BradleyEdino表示:

    en kazancl? slot oyunlar? slot siteleri en cok kazand?ran slot oyunlar?

  26. Wallacefex表示:

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

  27. Cell Phone: The Past, The Present And The Long Run 다바오다운로드

  28. Cory Burns表示:

    This was nicely structured. Discover more at عمل prk

發佈留言

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