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,431,272 Responses

  1. Ollie表示:

    15 Terms Everyone Working In The Bed And Couch Industry Should Know deep
    seat couch (Ollie)

  2. DevonPhack表示:

    https://pharm24.pro/# ed remedies that really work
    prescription meds without the prescriptions

  3. LeroyBak表示:

    indian pharmacy online Best online Indian pharmacy cheapest online pharmacy india

  4. I enjoy reading a post that can make men and women think.

    Also, many thanks for permitting me to comment!

  5. Hi, i read your blog occasionally and i own a similar one and i was just curious if you get a
    lot of spam remarks? If so how do you reduce it, any plugin or anything you can advise?
    I get so much lately it’s driving me insane so any support is
    very much appreciated.

  6. 20 Trailblazers Setting The Standard In Best Car Accident Lawyer Near Me Car accident and injury lawyers

  7. Trevor表示:

    5 Killer Quora Answers On Automatic Folding Electric Scooter automatic
    folding electric scooter (Trevor)

  8. 10 Mobile Apps That Are The Best For French Door Fridge On Sale french door fridge without ice maker

  9. This post is priceless. When can I find out more?

    https://databullseye2.ambarita.org/

  10. 20 Things You Should Be Educated About Best Car Accident Lawyers attorney for Car accident Injury

  11. sykaaa casino表示:

    Чтобы получить презент, необходимо внести на баланс минимум 500
    рублей.

  12. 10 Real Reasons People Dislike Pragmatic Authenticity Verification Pragmatic Authenticity Verification 프라그마틱 정품 확인법

  13. Fridge Freezer Cheap Tools To Improve Your Daily Life Fridge Freezer Cheap Trick That Everyone Should Be Able To fridge Freezer cheap

  14. AbecPaish表示:

    Successful treatment is available when you Viagra Oral Jelly online you should consult your physician.

  15. Are You Responsible For An Double Double Bunk Beds Budget?
    10 Terrible Ways To Spend Your Money double bunk bed cheap

  16. It’s very straightforward to find out any matter on net as compared to
    books, as I found this article at this web site.

  17. 7 Things About Sports Toto Official Website You’ll Kick Yourself
    For Not Knowing 토토사이트 순위 (Sportstoto50387.buscawiki.Com)

  18. Diplomi_wuoi表示:

    купить официально диплом о высшем prema-diploms.ru .

  19. BrandonSaisy表示:

    top 10 pharmacies in india: indian pharm 24 – world pharmacy india

  20. Guide To Electric Patio Heaters Uk: The Intermediate Guide
    In Electric Patio Heaters Uk Electric patio heaters UK

  21. Why People Don’t Care About Car Accidents Lawyers Near Me Top Rated Car Accident Attorney

  22. Wow, amazing blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your web site is excellent, let alone
    the content!

  23. So , You’ve Purchased Replacement Key For Audi A3 …

    Now What? audi car key replacement cost (https://auto-locksmiths10548.dreamyblogs.com)

  24. Jorgeagopy表示:

    top 10 pharmacies in india: Indian pharmacy international shipping – indian pharmacy

發佈留言

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