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,235,500 Responses

  1. Karri表示:

    What’s The Most Creative Thing That Are Happening With Mesothelioma Law mesothelioma legal (Karri)

  2. gay_kspr表示:

    Лучшие спортивные ставки на гей-тематику | Спортивные ставки для LGBTQ+ на 1xbet | Полезная информация о 1xbet для геев | Как делать ставки на 1xbet, если вы гей | Гид по ставкам на 1xbet для LGBTQ+
    gay ponr in 1xbet https://1xbetcasinogayclub-ar.com/ .

  3. Darrelherse表示:

    deneme bonusu veren siteler: casino slot siteleri – en iyi slot siteleri

  4. KevinSounc表示:

    https://slotsiteleri.bid/# slot siteleri

  5. Les algorithmes de recommandation utilisent des techniques de machine learning pour analyser le comportement de navigation des utilisateurs et leur proposer des recommandations personnalisées en temps réel.

  6. 20 Reasons To Believe Mesothelioma Law Firm
    Will Not Be Forgotten Mesothelioma Legal specialist

  7. The Reasons Mesothelioma Attorney Is Fast Becoming The
    Hottest Fashion Of 2023 mesothelioma law Firms

  8. Darrelherse表示:

    en yeni slot siteleri: slot siteleri guvenilir – slot oyun siteleri

  9. Darrelherse表示:

    casino slot siteleri: 2024 en iyi slot siteleri – slot siteleri

  10. ev taşıma表示:

    Ev Taşıma | Ev taşıma sürecinde dikkat edilmesi gereken noktalar hakkında bilgilendirici bir makale olmuş. Kozcuoğlu Ev Taşıma olarak, müşterilerimize her aşamada rehberlik etmeye hazırız.

  11. KevinSounc表示:

    https://denemebonusuverensiteler.win/# deneme bonusu veren siteler

  12. memek表示:

    If you wish for to improve your experience only keep visiting this site and
    be updated with the newest news update posted here.

  13. RSS reader表示:

    My family all the time say that I am killing my
    time here at net, except I know I am getting know-how all the
    time by reading thes nice articles or reviews.

  14. Darrelherse表示:

    slot kumar siteleri: slot oyun siteleri – en iyi slot siteleri

  15. Seat Car Key表示:

    See What Seat Car Key Cover Tricks The Celebs Are Making Use Of Seat Car Key

  16. Randallabevy表示:

    vavada рабочее зеркало на сегодня vavada рабочее на сегодня

  17. KevinSounc表示:

    https://denemebonusuverensiteler.win/# bonus veren siteler

  18. tiger168表示:

    We’re a bunch of volunteers and starting a new scheme in our community.

    Your site offered us with helpful info to work on. You have
    performed an impressive activity and our entire neighborhood will likely be
    thankful to you.

  19. Quality articles or reviews is the secret to be a focus for the visitors to go to see the
    web site, that’s what this website is providing.

發佈留言

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