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,154,414 Responses

  1. How A Weekly Online Shopping Figures Uk Project Can Change Your Life Skip Hop Zoo Straw Bottle Bee

  2. 7 Secrets About Why Are CSGO Skins Going Up In Price That Nobody Can Tell
    You cs2 cases (https://secure.Suchna.com)

  3. Alphonso表示:

    Slot Volatility Explained In Fewer Than 140 Characters evoplay
    slots symbols (Alphonso)

  4. The 10 Most Terrifying Things About Medical Malpractice Attorneys medical malpractice

  5. Rhea表示:

    Many thanks! Awesome stuff!

  6. w.okhy.Com表示:

    “Ask Me Anything:10 Answers To Your Questions About Best Slot Machines Multi-line slots [w.okhy.Com]

  7. elsycrays.top表示:

    This Is How Locksmith For Cars Will Look In 10 Years Time elsycrays.top

  8. wonderful put up, very informative. I ponder why the other
    specialists of this sector don’t realize this. You must proceed your writing.
    I’m sure, you have a huge readers’ base already!

  9. link bokep表示:

    I’m not sure why but this website is loading very slow for me.

    Is anyone else having this problem or is it a problem on my end?
    I’ll check back later and see if the problem still exists.

  10. Clarita表示:

    A Peek At The Secrets Of Three Wheel Mobility Scooters
    Sale folding mobility scooters for sale near me (Clarita)

  11. Drug information. Drug Class.
    buy effexor without prescription
    Some news about medicines. Read now.

  12. u.to表示:

    You’ll Never Be Able To Figure Out This Window Replacement Near Me’s Tricks Window replacement
    near me [u.to]

  13. The 10 Most Scariest Things About Fela Railroad Settlements fela railroad settlements

  14. Guide To Double Glazing Near Me: The Intermediate Guide Towards Double Glazing Near Me
    double glazing near me

  15. Hi there! I know this is kind of off topic but I was wondering if you
    knew where I could get a captcha plugin for my comment form?

    I’m using the same blog platform as yours and I’m having problems finding one?
    Thanks a lot!

  16. What i don’t understood is in reality how you’re now not really a lot
    more smartly-appreciated than you may be now. You’re very
    intelligent. You understand therefore significantly with regards to
    this topic, made me for my part imagine it from numerous numerous angles.
    Its like men and women are not involved unless it is one thing to do with Lady gaga!
    Your personal stuffs nice. Always deal with it up!

  17. Many Of The Common Errors People Make When Using Auto Accident Attorneys auto accident law firms

  18. 출장안마表示:

    I do accept as true with all the ideas you have introduced
    for your post. They’re really convincing and will certainly work.
    Nonetheless, the posts are too brief for novices. Could you please lengthen them a little from next time?
    Thanks for the post.

    Take a look at my webpage – 출장안마

  19. Игроки смогут поделиться своими впечатлениями
    об обслуживании в Лев казино, оставляя отзывы на специализированных площадках и форумах.

  20. Tauslot表示:

    My brother suggested I would possibly like this web
    site. He used to be entirely right. This put up actually made my
    day. You can not imagine just how much time I had spent for this information! Thanks!

  21. 10 Websites To Help You Develop Your Knowledge About Slot Machine Games Newest Slots (M.Coniconimall.Com)

  22. hey表示:

    If some one needs expert view regarding blogging after that i propose him/her to pay a quick visit this web site, Keep up the fastidious work.

  23. hi!,I like your writing very a lot! share we keep in touch
    extra about your post on AOL? I need an expert in this space to resolve my problem.
    May be that’s you! Looking forward to peer you.

發佈留言

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