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,096 Responses

  1. Candace表示:

    20 Fun Facts About Affordable SEO Services Near Me affordable seo consultant (Candace)

  2. 5 Upvc Windows And Doors Near Me Tips From The Pros
    upvc Windows Repair near Me

  3. A片表示:

    A片

    Having read this I thought it was extremely informative.
    I appreciate you finding the time and energy to put this content together.
    I once again find myself personally spending a significant amount of
    time both reading and posting comments. But so
    what, it was still worth it!

  4. 10 Unquestionable Reasons People Hate Seo Search Engine Optimisation Search Engine optimization Packages [maps.google.dm]

  5. Danieldob表示:

    Всем привет! Может кто знает, где почитатьполезные блоги о недвижимости? Сейчас читаю https://universal37.ru

  6. Window Repair表示:

    An Intermediate Guide To Repair Upvc Window Window Repair

  7. The 10 Most Scariest Things About Best Single Stroller
    2024 best Single stroller 2024

  8. Noe表示:

    Hi, I do believe this is a great website. I stumbledupon it 😉 I’m going to return yet again since I book-marked it.

    Money and freedom is the greatest way to change, may you be
    rich and continue to help others.

  9. I loved as much as you’ll receive carried out right here.

    The sketch is tasteful, your authored material stylish.
    nonetheless, you command get bought an shakiness over that you wish
    be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly very often inside case you
    shield this hike.

    Feel free to visit my blog :: the genius wave reviews

  10. Ten 18 Wheeler Accident Law Firms That Really Help You Live Better 18 wheeler Accident law firms

  11. I am truly glad to read this weblog posts which contains tons of helpful data, thanks for providing these
    data.

  12. Ricky表示:

    5 Killer Quora Answers To Popular Casino Slots popular casino slots (Ricky)

  13. 10 Great Books On Replacement Upvc Window Handles upvc window repair near me

  14. After checking out a handful of the blog articles on your blog, I
    really like your way of writing a blog. I added it to my bookmark webpage list and will be checking back soon. Take a
    look at my web site too and tell me what you think.

    Feel free to surf to my web-site – boostaro price

  15. Why Dangerous Drugs Isn’t As Easy As You Imagine Dangerous drugs Lawyers

  16. Madelaine表示:

    Need Inspiration? Check Out Drive Folding Mobility Scooter scooter folds (Madelaine)

  17. Introduction: Chill Cooler Portable AC air
    conditioning units have become increasingly popular for individuals seeking comfort in various settings, including offices,
    bedrooms, and outdoor spaces.

  18. Whitney表示:

    I’m gone to convey my little brother, that he should also visit this weblog on regular basis
    to get updated from latest information.

  19. I every time emailed this weblog post page to all my friends, for the
    reason that if like to read it next my friends will too.

  20. 9 Lessons Your Parents Teach You About Slot Tournaments Slot Tournaments

  21. The 10 Most Terrifying Things About Truck Accident Law Firm truck accident law firm

  22. 20 Trailblazers Lead The Way In Slot Updates Classic slots (http://www.rogeryamashita.com)

發佈留言

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