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,234,616 Responses

  1. 15 Gifts For The Audi A4 Car Key Replacement Lover In Your
    Life Audi Key Replacement Cost

  2. What’s Holding Back This Fold Up Electric Wheelchair Industry?
    compact folding electric Wheelchair

  3. ClydePab表示:

    en kazancl? slot oyunlar?: az parayla cok kazandiran slot oyunlar? – en kazancl? slot oyunlar?

  4. Reginald表示:

    20 Resources That’ll Make You More Effective At ADHD Private Diagnosis private
    adult adhd assessment belfast – Reginald,

  5. LarryCoell表示:

    slot tr online slot siteleri az parayla cok kazandiran slot oyunlar?

  6. CarrollUnton表示:

    deneme bonusu veren siteler denemebonusu2026.com http://slot-tr.online/# slot oyunlar?

  7. porn表示:

    Good info. Lucky me I found your website by accident (stumbleupon).

    I’ve saved as a favorite for later!

  8. ClydePab表示:

    matadorbet giris: matadorbet bid – matadorbet bid

  9. Diplomi_tqkn表示:

    купить диплом в верхней пышме 1russa-diploms.ru .

  10. The 12 Worst Types Of Users You Follow On Twitter
    double Seat stroller

  11. What’s The Job Market For Accident Claims Lawyers Professionals?

    accident claims Lawyers (zenwriting.net)

  12. CarrollUnton表示:

    deneme bonusu veren siteler denemebonusu2026.com https://denemebonusuverensiteler.top/# deneme bonusu veren siteler denemebonusu2026.com

  13. Giselle表示:

    Guide To Organic Coffee Beans 1kg: The Intermediate Guide On Organic
    Coffee Beans 1kg organic coffee beans 1kg (Giselle)

  14. Hi there, of course this paragraph is actually good and I have learned lot of things from it
    about blogging. thanks.

    Also visit my web blog; รับจัดงานแต่งงาน

  15. Diplomi_baMl表示:

    купить диплом о образовании в москве цена купить диплом о образовании в москве цена .

  16. situs 4D表示:

    Guide To Situs 4d: The Intermediate Guide To Situs 4d situs 4D

  17. What’s up, I read your blogs like every week. Your writing style is awesome, keep up the good work!

  18. Derekfar表示:

    ultrabet yeni giris 1125: ultrabet – ultrabet bonus

  19. This piece of writing presents clear idea in support
    of the new viewers of blogging, that in fact how to
    do running a blog.

  20. You could certainly see your enthusiasm in the article you write.
    The arena hopes for even more passionate writers like you who are not afraid to mention how they believe.
    At all times follow your heart.

  21. Derekfar表示:

    casino siteleri win: guvenilir casino siteleri – Canl? Casino Siteleri

  22. Derekfar表示:

    guvenilir casino siteleri: guvenilir casino siteleri – casino siteleri win

  23. LarryCoell表示:

    slot tr online en kazancl? slot oyunlar? slot oyunlar? puf noktalar?

  24. Andra表示:

    What’s The Job Market For Distressed Leather Couch Professionals?
    distressed leather couch (Andra)

  25. ai-porn.fans表示:

    Awesome! Its truly remarkable article, I have got much clear
    idea concerning from this paragraph.

  26. Five Killer Quora Answers On Automatic Folding Scooter Mobility automatic folding Scooter Mobility

  27. You can definitely see your enthusiasm within the work you write.
    The arena hopes for more passionate writers like you who are not afraid to
    say how they believe. Always follow your heart.
    https://paitosgpwarna.net/

  28. Тут можно преобрести где купить сейф для оружия сейфы для оружия цены

發佈留言

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