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,233,809 Responses

  1. 9 Things Your Parents Taught You About Replace Window Handles replace window handles

  2. Your Family Will Be Grateful For Getting This Work From Home Jobs Part
    Time Work From Home Jobs Near Me

  3. The Personal Injury Litigation Case Study You’ll Never Forget personal injury law Firms

  4. You have made some good points there. I checked on the internet for
    additional information about the issue and found most people will go along with your views on this web site.

  5. Davidtes表示:

    cytotec pills buy online buy cytotec online fast delivery cytotec abortion pill

  6. Xiomara表示:

    Mesothelioma Litigation Tools To Facilitate Your Day-To-Day Life asbestos case (Xiomara)

  7. WilliamRhype表示:

    https://cytotec.club/# cytotec abortion pill

  8. Why Everyone Is Talking About Combination Washer Dryer In One Right Now Best Washer Dryer (http://Www.Stes.Tyc.Edu.Tw/)

  9. Davidtes表示:

    what is tamoxifen used for tamoxifen pill does tamoxifen make you tired

  10. 15 Trends That Are Coming Up About Tier 2
    Seo tiered link building strategy

  11. Where Can You Find The Most Effective Heat Pump Tumble Dryer Uk Information?
    heat pump tumble dryer reviews

  12. A Look At The Secrets Of Window Repair Near Window Repair Near me

  13. 9 . What Your Parents Taught You About Boat
    Accident Lawsuit Boat Accident lawsuit

  14. You’ll Be Unable To Guess Small Treadmill With Incline’s
    Tricks small treadmill With incline

  15. Davidtes表示:

    cipro 500mg best prices buy cipro online without prescription buy cipro cheap

  16. Ronaldpsymn表示:

    http://finasteride.store/# cost generic propecia price

  17. RandallTaulp表示:

    order cheap propecia pill: cost propecia no prescription – buying generic propecia no prescription

  18. Methaqualone, like Quaaludes, is a prescription sedative that
    increases the activity of GABA (neurotransmitter) receptors.

  19. Ronaldpsymn表示:

    https://ciprofloxacin.tech/# buy cipro online canada

  20. WilliamRhype表示:

    http://lisinopril.network/# lisinopril 3760

  21. 25 Unexpected Facts About Double Memory Foam Mattresses double Mattress in sale

  22. Ronaldpsymn表示:

    http://cytotec.club/# Cytotec 200mcg price

  23. Davidtes表示:

    ciprofloxacin 500mg buy online buy cipro online ciprofloxacin generic

  24. 14 Cartoons About Hiring Car Accident Attorneys That’ll Brighten Your Day houston car accident
    attorney (fuglsang-steen.blogbright.net)

  25. Good post. I learn something totally new and challenging on websites I stumbleupon everyday.
    It’s always helpful to read through articles from other writers and practice a little something from their sites.

  26. This Is The Advanced Guide To Defra Exempt Stoves DEFRA registered stoves

  27. service表示:

    5 Double Glazed Windows Repairs Lessons From The Pros service

  28. Davidtes表示:

    zestril 10mg lisinopril 2.5 cost lisinopril 40 mg no prescription

發佈留言

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