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,477,645 Responses

  1. Marcus Curry表示:

    Pressure washing is such an essential part of home maintenance that often gets overlooked! Thanks for pointing me towards Best Pressure Washing

  2. Austin Tucker表示:

    Great insights! Find more at commercial scaffolding

  3. Jacobwaync表示:

    The 1win promo code: MAX500WIN. This 1Win bonus code 2025 rewards new players with a 500% bonus up to $1025. Valid codes for receiving bonuses without a deposit for new and old users. Hurry to get them today! Activate the promo code 1win and get a generous welcome bonus of up to 500% on your first four deposits.

    1win bonus code free

  4. Donaldabula表示:

    Tadalafil Tablet Buy Tadalafil 10mg Cialis 20mg price

  5. BrettTIT表示:

    Generic Tadalafil 20mg price: п»їcialis generic – cheapest cialis

  6. BrettTIT表示:

    cost of ed meds: FastPillsEasy – low cost ed meds online

  7. Nell Soto表示:

    I love that there’s a dedicated conversation around home care in Baltimore; it’s so necessary! home care

  8. Donaldabula表示:

    cialis for sale MaxPillsForMen Buy Cialis online

  9. RogerFaf表示:

    http://fastpillsformen.com/# Cheap Sildenafil 100mg

  10. A well-structured website can significantly improve SEO performance. Your insights here are invaluable! Learn more at seo company nashville

  11. Incredible a lot of superb material.

  12. Charlesbot表示:

    https://fastpillseasy.com/# ed meds by mail

  13. Hi there to every one, the contents existing at this web page are really remarkable for people knowledge, well, keep up the nice work fellows.

  14. Barrybib表示:

    online ed meds: FastPillsEasy – ed online prescription

  15. goldpay表示:

    The Benefits Associated With Home Based Business For Work-At-Home Moms goldpay

  16. Charlesbot表示:

    https://fastpillsformen.com/# Buy Viagra online cheap

  17. ulfdgr表示:

    寻找智能输入法?立即下载搜狗输入法,适配多种设备平台,支持拼音、五笔和语音输入,兼具快速精准与智能联想功能。个性化设置、丰富表情包和多语言支持让输入更便捷有趣。通过搜狗输入法下载,获得专业级输入体验,轻松应对办公、学习与娱乐场景的多种需求!https://shurufa-sogou.com/         zmxso

  18. retro bowl 2表示:

    This blog was… how do I say it? Relevant!! Finally I’ve found something which helped me.
    Cheers!

  19. whoah this weblog is fantastic i love studying your articles.
    Keep up the good work! You already know, many individuals are looking around
    for this info, you could aid them greatly.

  20. срочная наркологическая помощь в москве http://www.vishivayu.ukrbb.net/viewtopic.php?f=12&t=13503 .

發佈留言

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