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,364,333 Responses

  1. Veta Naugle表示:

    How To Outsmart Your Boss In Tree House Bunkbeds High sleeper treehouse bed (http://wx.Abcvote.cn/)

  2. BarryDof表示:

    http://ozempic.art/# buy ozempic

  3. Tracee Nibbi表示:

    5 Killer Quora Answers On Integral Fridge Freezer integral fridge freezer (Karol)

  4. В магазине сейфов предлагают купить сейф оптом заказать сейф

  5. Dotty Bristow表示:

    3 Ways In Which The Upvc Window Repairs Near Me Can Influence Your Life upvc windows repair near me

  6. Frankevild表示:

    https://ozempic.art/# ozempic cost

  7. В магазине сейфов предлагают сейфы купить в москве купить сейф интернете

  8. Very shortly this web page will be famous among all blog users, due to it’s pleasant articles or reviews

  9. The Worst Advice We’ve Seen About Car Boot Mobility Scooter Car Boot Mobility Scooter lightweight Collapsible mobility scooters

  10. В магазине сейфов предлагают купить сейф оптом сейф цена купить

  11. Seo London Uk表示:

    15 Things You Don’t Know About SEO London Seo London Uk

  12. Expert Advice On How To Check The Authenticity Of Pragmatic From A Five-Year-Old 프라그마틱 카지노

  13. Jeremypag表示:

    buy semaglutide pills rybelsus price rybelsus coupon

  14. Jeremypag表示:

    rybelsus price cheapest rybelsus pills semaglutide online

  15. Hi there! This blog post could not be written much better!
    Going through this article reminds me of my previous roommate!
    He always kept preaching about this. I will forward
    this information to him. Pretty sure he will have a great read.
    Many thanks for sharing!

  16. BarryDof表示:

    https://ozempic.art/# ozempic coupon

  17. 10 Things We All Love About Electric Wall.Mounted Fire best wall mounted electric fires (Brad)

  18. RamonRek表示:

    buy ozempic pills online: ozempic coupon – ozempic coupon

  19. срочный вывод из запоя на дому ростов срочный вывод из запоя на дому ростов .

  20. Jeremypag表示:

    semaglutide cost buy semaglutide pills rybelsus coupon

  21. I read this post fully on the topic of the comparison of most up-to-date and earlier technologies, it’s remarkable article.

  22. RamonRek表示:

    ozempic: ozempic online – buy cheap ozempic

  23. Guide To Buy Electric Mobility Scooters: The Intermediate Guide Towards Buy Electric Mobility Scooters Buy electric mobility scooters

  24. Hello to every single one, it’s actually a fastidious for me to pay a quick visit this website, it includes valuable Information.

  25. Just desire to say your article is as surprising. The clarity in your post is simply
    spectacular and i could assume you’re an expert on this subject.
    Well with your permission allow me to grab your feed to keep up to date with forthcoming post.
    Thanks a million and please continue the gratifying work.

  26. Jeremypag表示:

    ozempic generic ozempic generic ozempic online

  27. Tamela表示:

    Why Everyone Is Talking About Wall Mount Fireplace Today fireplace insert (Tamela)

發佈留言

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