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,449,330 Responses

  1. kypit 1s_zgEn表示:

    купить программу 1с бухгалтерия купить программу 1с бухгалтерия .

  2. 오피表示:

    Luxury Lounge 오피

  3. lesbian porn表示:

    Yes! Finally something about Porn.

  4. This piece of writing will assist the internet viewers for setting up new webpage or even a weblog from start to end.

  5. Edwardrob表示:

    вызвать девушек в калуге путаны калуги

  6. kypit 1s_tnEn表示:

    программы фирмы 1с программы фирмы 1с .

  7. Excellent way of describing, and pleasant post to get facts concerning my presentation focus, which i am going to deliver in university.

  8. вывод из запоя в стационаре воронежа vyvod-iz-zapoya-v-stacionare-voronezh23.ru .

  9. prostavive表示:

    When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each
    time a comment is added I get four e-mails with the same comment.

    Is there any way you can remove me from that service?
    Thanks!

  10. zaramea01表示:

    We’re a gaggle of volunteers and starting a new scheme in our community.
    Your web site provided us with useful information to work on. You have performed an impressive process and our whole community shall be thankful to you.

    my page :: zaramea01

  11. Hello! Would you mind if I share your blog with my facebook group?
    There’s a lot of folks that I think would really enjoy your content.
    Please let me know. Thank you

  12. Brandonnot表示:

    IndianCertPharm: IndianCertPharm – Indian pharmacy that ships to usa

  13. WilliamBak表示:

    Хотите купить окна мелке раздвижное окно по разумной цене? Ознакомьтесь с нашим предложением! У нас — качество, надежность и стиль по доступной стоимости. Индивидуальный подход к каждому заказу!

  14. Ten Easy Steps To Launch Your Own Best Home Vacuum Cleaner
    Business best Value robot vacuum

  15. CurtisBam表示:

    canada drug pharmacy: Canadian Md Pharm – best mail order pharmacy canada

  16. Nine Things That Your Parent Taught You About Blondie Scarlet Macaw Bird
    Blondie scarlet Macaw Bird

  17. kypit 1s_nror表示:

    1с бухгалтерия купить 1с бухгалтерия купить .

  18. Brandonnot表示:

    canada drugs online review: CanadianMdPharm – canadian pharmacy india

  19. kypit 1s_zror表示:

    настройка 1с бухгалтерии цена https://svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513 .

  20. WilliamBak表示:

    Хотите купить окна заказ окно мелке по разумной цене? Ознакомьтесь с нашим предложением! У нас — качество, надежность и стиль по доступной стоимости. Индивидуальный подход к каждому заказу!

  21. CurtisBam表示:

    Indian pharmacy international shipping: Indian Cert Pharm – Indian Cert Pharm

  22. Good site you have here.. It’s difficult to find excellent writing like yours nowadays.
    I seriously appreciate individuals like you! Take care!!

  23. kypit 1s_rzEn表示:

    купить 1с бухгалтерия купить купить 1с бухгалтерия купить .

發佈留言

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