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,145,918 Responses

  1. Estelle表示:

    Don’t Buy Into These “Trends” About Cheapest Online Grocery Shopping Uk cheap online grocery shopping uk
    (Estelle)

  2. 10 Apps To Help Control Your Slot Bonuses video Slots – https://firsturl.de/,

  3. Meds information for patients. Cautions.
    how to get cheap chlorpromazine for sale
    Everything trends of drugs. Read here.

  4. Best Slots表示:

    15 Top Pinterest Boards Of All Time About Unique Slots Best Slots

  5. slot features表示:

    A Guide To Classic Slots From Start To Finish slot features

  6. It is operated by Caesars Entertainment, 1 of the largest gambling chains in the US.

    my page :: http://seohyuneng.net/bbs/board.php?bo_table=free&wr_id=10199

  7. The 10 Most Terrifying Things About Fela Railroad Settlements Fela Railroad
    Settlements (https://Www.Cheaperseeker.Com)

  8. pvp777表示:

    It’s very effortless to find out any matter on net as compared to books, as I found this paragraph at this website.

  9. 7 Tricks To Help Make The Most Of Your Demo Slot Sugar sugar rush Slot demo by pragmatic Play

  10. Fela Railroad表示:

    See What Fela Railroad Tricks The Celebs Are Using
    Fela Railroad

  11. Don’t Buy Into These “Trends” Concerning Shopping Online
    Sites versatile mobile server rack

  12. Nine Things That Your Parent Teach You About Shopping Online Site Clothes shopping online site clothes –
    http://www.areumkids.com/board/bbs/board.php?bo_table=qna&wr_id=2377,

  13. linkvault.win表示:

    5 Lessons You Can Learn From Winning Slots real Money Slots (linkvault.win)

  14. What Is The Secret Life Of Online Shopping Uk seventh
    generation 12 pack (https://Vimeo.com)

  15. Hello, Neat post. There is a problem together with your site in internet explorer,
    may check this? IE nonetheless is the market chief
    and a large part of folks will pass over your magnificent writing due to this problem.

  16. You said it adequately..

  17. Hey I know this is off topic but I was wondering if you
    knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite
    some time and was hoping maybe you would
    have some experience with something like this. Please let me know if you run into
    anything. I truly enjoy reading your blog and I look forward to
    your new updates.

  18. These are in fact great ideas in about blogging. You have touched some nice points here.

    Any way keep up wrinting.

  19. Christine表示:

    The Three Greatest Moments In Slot Mechanics History top casino slots [Christine]

  20. bokep indo表示:

    Aw, this was an exceptionally nice post. Spending some time and actual effort to produce a good article… but what can I say… I procrastinate a
    whole lot and don’t seem to get anything done.

  21. Roseline Laird is what you are able call her and she totally
    loves this reputation. Minnesota is where
    our house and Dislike plan on changing that it. Interviewing is
    his day job now. One of his favorite hobbies is to cycle but he’s thinking on starting something newer.

  22. Why Nobody Cares About Professional Slots Slot Machine bonuses

  23. memek becek表示:

    Great weblog here! Also your web site lots up very fast!
    What host are you the usage of? Can I am getting your affiliate hyperlink for your host?
    I desire my web site loaded up as fast as yours lol

  24. 5 Clarifications On Progressive Jackpot Slots slots
    With bonuses (https://wiki.conspiracycraft.net/)

  25. lawyers表示:

    The 10 Most Scariest Things About Boat Accident Lawsuit lawyers

發佈留言

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