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,371,205 Responses

  1. Volkswagen Polo Key Price Tools To Improve Your Everyday Lifethe Only Volkswagen Polo Key Price Trick That
    Every Person Should Know volkswagen polo key price

  2. HaroldBig表示:

    rybelsus coupon: buy semaglutide pills – rybelsus price

  3. Frankevild表示:

    https://ozempic.art/# buy cheap ozempic

  4. Thanks , I’ve recently been searching for information about this topic for a while and
    yours is the greatest I have came upon till now. But,
    what about the bottom line? Are you positive concerning the supply?

  5. KennethBrode表示:

    просушка штукатурки https://sushka-pomeshchenij-v-msk.ru/

  6. Maurine表示:

    I’ll right away seize your rss feed as I can not find your e-mail subscription hyperlink or
    e-newsletter service. Do you’ve any? Kindly let me understand so that I may
    subscribe. Thanks.

  7. BarryDof表示:

    http://ozempic.art/# buy ozempic

  8. 5 Killer Quora Answers To Wall Mount Fireplace Stone Fireplaces

  9. BarryDof表示:

    https://ozempic.art/# buy cheap ozempic

  10. Jeremypag表示:

    ozempic online Ozempic without insurance ozempic coupon

  11. I am curious to find out what blog platform you’re using?
    I’m having some minor security problems with my latest site and I’d like to find something more secure.
    Do you have any suggestions?

  12. RamonRek表示:

    ozempic cost: buy cheap ozempic – ozempic

  13. RamonRek表示:

    ozempic online: ozempic online – Ozempic without insurance

  14. Halo 3: ODST 2009 first-individual shooter sport developed by Bungie.
    Published by Microsoft Game Studios. The fifth installment within the Halo franchise
    as a facet sport,[1] it was released on the Xbox 360 in September 2009.
    Players assume the roles of United Nations Space Command Marines, referred to as “Orbital Drop Shock Troopers” or ODSTs,
    during and after the occasions of Halo 2. In the sport’s campaign mode, players explore the ruined metropolis of new Mombasa to find what happened to their missing teammates within the midst of an alien invasion. Within the “Firefight” multiplayer choice, players battle increasingly tough
    waves of enemies to score points and survive
    so long as possible; Halo 3’s multiplayer is contained on a separate disc packaged with ODST.

  15. Lolita表示:

    Electric Fires Free Standing Explained In Fewer Than 140 Characters
    cheap free standing electric fires (Lolita)

  16. Tabitha表示:

    The 10 Most Scariest Things About ADHD Adult Test adhd adult test – Tabitha

  17. HaroldBig表示:

    ozempic cost: ozempic online – ozempic cost

  18. I’ve been browsing on-line greater than 3 hours nowadays, yet I by no
    means discovered any fascinating article like yours.
    It is pretty price sufficient for me. Personally, if all
    site owners and bloggers made good content material as you did, the net
    shall be a lot more useful than ever before.

  19. HaroldBig表示:

    ozempic online: ozempic online – ozempic coupon

  20. Jeremypag表示:

    semaglutide online rybelsus price rybelsus coupon

  21. HaroldBig表示:

    rybelsus cost: rybelsus pill – buy semaglutide online

  22. Mostbet DE表示:

    What’s up, this weekend is fastidious designed for me, as this moment i am reading this wonderful educational post here at my house.

    Visit my homepage … Mostbet DE

  23. Frankevild表示:

    https://ozempic.art/# Ozempic without insurance

  24. Polish Your Image With Online Reputation Management 링크모음 사이트 (verbina-glucharkina.ru)

  25. togel4D表示:

    7 Things You’ve Never Known About Bandar Online Togel togel4D

  26. Gartandabsex表示:

    Веселые анекдоты
    Подними себе настроение

  27. Gartandabsex表示:

    Лучшие анекдоты
    Подними себе настроение

  28. A Step-By’-Step Guide For Audi Advanced Key audi tt replacement key

發佈留言

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