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,153,594 Responses

  1. Play Slots表示:

    5 Killer Quora Answers To Play Slots Play Slots

  2. Buzzwords De-Buzzed: 10 Different Ways To Deliver Private Mental Health Assessment Near Me Mental health Assesment

  3. 11 Creative Methods To Write About Jackpot Slots Random Jackpot slots

  4. Starla表示:

    I’m pretty pleased to discover this website. I want
    to to thank you for ones time just for this wonderful read!!

    I definitely loved every little bit of it and i also have you
    book marked to look at new stuff in your site.

  5. gsa ser表示:

    You’ll Be Unable To Guess GSA SER Coupon’s Tricks gsa ser

  6. The Top Reasons Why People Succeed In The SEO Software UK Industry seo traffic software

  7. sex children表示:

    This is my first time pay a quick visit at here and i am actually impressed to read everthing at single place.

  8. I used to be suggested this web site by way of my cousin. I am no longer
    certain whether or not this publish is written via him as nobody else understand such
    distinctive about my trouble. You are incredible!
    Thank you!

    my web page – fitspresso reviews

  9. What’s up to all, how is everything, I think every one is getting more from this site,
    and your views are fastidious in support of new people.

  10. Hi to every one, the contents existing at this site are truly remarkable for people experience,
    well, keep up the nice work fellows.

  11. 5 Tools That Everyone Is In The Slot Gaming Experience Industry Should Be Using best payouts slots

  12. Car accident表示:

    What’s The Job Market For Car Accident Attorney Professionals Like?
    Car accident

  13. 15 Things You’re Not Sure Of About New Slots Online reputable Slots

  14. Feel free to visit my site – craiglistforsex

  15. How Much Do SEO Companies Near Me Experts Earn? near me seo strategy

  16. Danieldob表示:

    Приветствую. Подскажите, где почитатьполезные блоги о недвижимости? Пока нашел https://universal37.ru

  17. That is a great tip particularly to those fresh to the
    blogosphere. Simple but very precise info… Appreciate your sharing this one.
    A must read article!

  18. 10 Motor Vehicle Settlement-Friendly Habits To Be Healthy motor vehicle accident lawyer

  19. I don’t know wether it’ѕ jᥙst me or if everyolne еlse encountering рroblems wth y᧐ur site.
    Ιt ѕeems ⅼike sоme of the written ttext іn yοur
    posts ɑre running off the screen. Can somеbody else
    please comment and let me knoԝ if tbis is happening tօ them tоo?
    Thhis miցht be a issue ѡith mmy internet browser Ьecause I’ve hhad thіs happpen pгeviously.
    Apprеciate it

    Also visit my homepagе Arena333 Slot Login Link Alternatif

  20. Birth Injury表示:

    Birth Injury Lawyers Tools To Streamline Your Daily Life Birth Injury Lawyers Technique Every Person Needs To Know Birth Injury

  21. seo表示:

    You’ll Never Be Able To Figure Out This Ecommerce
    SEO Agency’s Tricks seo

  22. zackfoxworth表示:

    This Is The History Of Treadmill Incline Foldable In 10 Milestones zackfoxworth

發佈留言

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