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,519,754 Responses

  1. You’ll Never Guess This Adult Adhd Assessment Uk’s Tricks Adhd Symptoms Checklist

  2. Pram 2In1表示:

    Pram 2in1 Tools To Ease Your Daily Life Pram 2in1 Trick That Every Person Must Be Able To Pram 2In1

  3. replacement表示:

    The Hidden Secrets Of Misted Double Glazing Repairs Near Me replacement

  4. How Pragmatic Slot Experience Has Become The Top Trend In Social Media 프라그마틱 슬롯 추천

  5. 15 Unquestionably Reasons To Love Private Diagnosis Of ADHD Private Adhd Assessment Sheffield Cost

  6. I feel that is one of the such a lot vital info for me. And i am happy reading your article.
    However wanna statement on some common things, The site taste is great, the articles
    is really excellent : D. Excellent task, cheers

  7. BrandonSaisy表示:

    top 10 pharmacies in india: Indian pharmacy worldwide delivery – online pharmacy india

  8. 20 Up And Coming 3 Wheel Pushchairs Stars To Watch The 3 Wheel Pushchairs Industry 3 Wheeled Pushchairs

  9. LeroyBak表示:

    ed pills online pharmacy cheap pharmacy how to get prescription drugs without doctor

  10. macauslot88表示:

    We’re a group of volunteers and opening a new scheme
    in our community. Your web site provided us with valuable info to work
    on. You’ve done a formidable job and our entire community will be
    grateful to you.

  11. 10 Tips For Quickly Getting Treadmill That Folds Flat foldable treadmill

  12. A Cheat Sheet For The Ultimate For Tree House Twin Bunk Bed treehouse Bunk Bed uk

  13. 9 Signs That You’re A Pragmatickr Expert 프라그마틱 슬롯 조작

  14. Diplomi_rvel表示:

    купить диплом повара 5 разряда man-diploms.ru .

  15. Wow that was odd. I just wrote an incredibly long comment
    but after I clicked submit my comment didn’t show up.
    Grrrr… well I’m not writing all that over again. Anyways,
    just wanted to say excellent blog!

  16. How To Create An Awesome Instagram Video About 2 In 1
    Pram And Car Seat Best 2 in-1 prams

  17. Wonderful blog! I found it while surfing around on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Many thanks

  18. Don’t Believe These “Trends” About Audi Keys Replacement key audi

  19. 9 . What Your Parents Taught You About 2 Seater Fabric Sofa Sale 2 seater fabric sofa

  20. Paito HK 6D表示:

    Wow that was odd. I just wrote an extremely long comment but after I clicked submit my comment didn’t show up.
    Grrrr… well I’m not writing all that over
    again. Regardless, just wanted to say great blog!

    https://w2.livedrawhk.uno/

  21. 4 Dirty Little Details About Private ADHD Assessment UK And
    The Private ADHD Assessment UK Industry private adhd assessment west yorkshire (onlybookmarkings.com)

  22. Useful info. Fortunate me I found your website by accident, and I am
    stunned why this accident did not happened in advance!
    I bookmarked it.

  23. 5 Lessons You Can Learn From Treadmill That Folds Flat speed and incline

  24. I really love your website.. Great colors & theme.

    Did you create this web site yourself? Please reply back as I’m wanting to
    create my own website and want to learn where you got this from
    or what the theme is named. Many thanks!

  25. DevonPhack表示:

    http://mexicanpharm24.cheap/# mexico drug stores pharmacies
    ed treatment

  26. LeroyBak表示:

    Online medicine order Indian pharmacy worldwide delivery india pharmacy mail order

  27. Williamagric表示:

    ed drugs https://indianpharm24.pro/# india pharmacy mail order

  28. Williamagric表示:

    cialis without doctor prescription https://indianpharm24.pro/# mail order pharmacy india

發佈留言

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