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,265,804 Responses

  1. Jamesdiure表示:

    пин ап зеркало: пин ап казино – пин ап казино

  2. Nikefut表示:

    how to get on dark web dark web market list dark market link

  3. Pol88表示:

    Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! However, how could
    we communicate?

  4. Josephmax表示:

    https://vavada-kazi.ru/# вавада казино зеркало

  5. Invece, i casinò non AAMS offrono spesso al giocatore un bonus di registrazione, senza che abbia ancora versato alcunché.

  6. Callie Wong表示:

    Everyone needs a corner of greenery where they can unwind; big thanks goes out to all seasons l landscape gardeners in Abingdon

  7. DarioTob表示:

    пин ап кз: пин ап кз – pinup

  8. CHNDRKIY5表示:

    I’m really enjoying the design and layout of your site. It’s a very
    easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a developer to create your theme?
    Exceptional work!

  9. DarioTob表示:

    вавада казино онлайн: вавада казино зеркало – vavada kazi

  10. You’ll Never Be Able To Figure Out This Togel Resmi Indonesia’s Tricks Togel resmi Indonesia

  11. Davao表示:

    Casino Blackjack Tips Davao

  12. Nikefut表示:

    dark web market dark market onion deep web drug store

  13. Hello! I’ve been following your web site for a long time now and finally got the bravery to go ahead and give you a shout out
    from Austin Tx! Just wanted to tell you keep up the excellent job!

    Visit my page :: best realtor in st johns county fl

  14. Eva Fields表示:

    So many options to choose from when planning your trip through a great agency like buggy in dubai #

  15. It’s awesome best realtor in volusia county fl
    support of me to have a website, which is beneficial in support of my knowledge.
    thanks admin

  16. DarioTob表示:

    пинап казино: пин ап зеркало – пин ап вход

  17. obviously like your website however you need to test
    the spelling on quite a few of your posts. A number of them are rife with spelling issues and I best realtor in tucson arizone
    finding it very bothersome to tell the truth then again I will certainly come again again.

  18. HenrydeT表示:

    пин ап казино официальный сайт pinup kazi пин ап вход

發佈留言

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