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,428,448 Responses

  1. Very rapidly this web site will be famous amid all blogging visitors, due to it’s good articles or reviews

  2. site表示:

    It’s very simple to find out any topic on web as
    compared to books, as I found this post
    at this website.

  3. It’s great that you are getting ideas from this piece of writing as well as from our argument made
    here.

  4. Геймеры могут играть с любых портативных устройств прямо в браузере.

  5. Larae表示:

    Small Gas Patio Heater Techniques To Simplify Your Daily Lifethe One Small Gas Patio Heater Technique Every Person Needs To Know small
    gas patio heater – Larae,

  6. So You’ve Bought Private ADHD Assessment London … Now What?
    private adhd assessment reading (privateadhdassessment68022.yomoblog.com)

  7. You Are Responsible For An Key Fob Repair Budget? 12 Top Notch Ways To Spend Your Money Repair Car
    Key Fob (http://Autoban.Lv)

  8. Williamagric表示:

    herbal remedies for ed http://pharm24.pro/# ed in men

  9. Also visit my web blog; Explore Daycares Locations
    (http://anticonvulsants.com/)

  10. Excellent way of telling, and good post to obtain data regarding my presentation subject matter, which i
    am going to present in institution of higher education.

  11. Williamagric表示:

    ed dysfunction http://pharm24.pro/# erectile dysfunction drug

  12. Thanks for the good writeup. It in fact was a leisure account
    it. Glance complicated to more brought agreeable from you!
    However, how can we keep up a correspondence?

  13. Are You Getting Tired Of Replacement Glass? 10 Inspirational Sources That
    Will Bring Back Your Love glass window replacement cost

  14. What’s The Current Job Market For L Shaped Sofa Couch Professionals?
    L Shaped Sofa couch

  15. 16 Facebook Pages You Must Follow For Mystery Box-Related Businesses best mystery box online shop

  16. New Audi Keys表示:

    Why New Audi Key Should Be Your Next Big Obsession New Audi Keys

  17. Five Killer Quora Answers On Sofas 2 Seater Fabric sofas 2 seater Fabric

  18. Rodney表示:

    9 Signs That You’re The Sprt Toto 4d Expert 첫가입 꽁머니, Rodney,

  19. Why viewers still use to read news papers when in this technological globe all is accessible on web?

    https://dtsg.drawsgp.info/

  20. LeroyBak表示:

    top online pharmacy india Best Indian pharmacy top 10 online pharmacy in india

  21. азино777表示:

    Не прошедшие отбор сайты вносятся в Единый реестр запрещенной информации.

  22. BrandonSaisy表示:

    indianpharmacy com: Best online Indian pharmacy – world pharmacy india

  23. Leather Couches For Sale Tools To Streamline Your Daily Lifethe One
    Leather Couches For Sale Trick That Should Be Used By Everyone Be Able To leather Couches for sale

發佈留言

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