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,448,168 Responses

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

  2. CurtisBam表示:

    Online medicine: Online pharmacy – Online medicine

  3. Ryder表示:

    A Guide To Asbestos Cancer Attorney From Beginning To End mesothelioma attorneys; Ryder,

  4. kypit 1s_qgEn表示:

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

  5. Avotexum App表示:

    Nice post. I was checking continuously this blog and I’m impressed!
    Very helpful info specifically the last part 🙂 I care for such info a lot.
    I was looking for this particular information for a long time.

    Thank you and best of luck.

  6. We stock smart phones from all the huge brands,
    consisting of Apple, Samsung, ZTE, Motorola, Oppo, Alcatel, Nokia & More.

  7. CurtisBam表示:

    canadian family pharmacy: CanadianMdPharm – best canadian online pharmacy reviews

  8. 10 Tips For New Driver’s License That Are Unexpected tappat körkort beställa nytt

  9. I would like to thank you for the efforts you’ve put
    in penning this blog. I am hoping to check out the same high-grade blog posts by you later
    on as well. In truth, your creative writing abilities has inspired me to get
    my very own site now 😉

  10. kypit 1s_mhEn表示:

    купить 1с бухгалтерия 8.3 проф версия цена купить 1с бухгалтерия 8.3 проф версия цена .

  11. kypit 1s_nmor表示:

    программа 1с купить с установкой программа 1с купить с установкой .

  12. CurtisBam表示:

    indian pharmacy: top 10 pharmacies in india – indian pharmacy

  13. DominicSpurl表示:

    дизайнер интерьера цена за услуги москва https://dizayn-interera213.ru

  14. Brandonnot表示:

    IndianCertPharm: IndianCertPharm – Online pharmacy

  15. 20 Things You Need To Be Educated About Asbestos Defense Attorney mesothelioma Lawyer

  16. kypit 1s_dnor表示:

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

  17. kypit 1s_hsEn表示:

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

  18. kypit 1s_xior表示:

    программа 1с купить с установкой программа 1с купить с установкой .

  19. An impressive share! I’ve just forwarded this onto a coworker who
    had been doing a little homework on this. And he
    actually bought me lunch simply because I discovered
    it for him… lol. So let me reword this…. Thank YOU for the meal!!
    But yeah, thanks for spending time to discuss this subject here on your website.

  20. kypit 1s_rlor表示:

    1с предприятие купить программу http://svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513/ .

  21. How To Explain SEO Services Agency To Your Mom Ecommerce Seo agency

  22. A Peek Inside The Secrets Of Evolution Blackjack 에볼루션 무료체험

  23. kypit 1s_icor表示:

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

  24. Right here is the perfect site for anybody who wishes to find out about this topic.
    You understand so much its almost tough to argue with you (not that I actually
    will need to…HaHa). You definitely put a brand
    new spin on a subject that’s been written about for a long time.
    Great stuff, just excellent!

  25. Продажа качественных дженериков для повышения потенции на сайте https://jeneri.ru/
    доставка во все регионы России

  26. kypit 1s_fxor表示:

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

  27. kypit 1s_jnEn表示:

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

  28. CurtisBam表示:

    ed meds online canada: best canadian online pharmacy – canadian mail order pharmacy

發佈留言

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