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,366,134 Responses

  1. Davidtes表示:

    buying cheap propecia no prescription cost cheap propecia no prescription cost generic propecia for sale

  2. RandallTaulp表示:

    tamoxifen chemo: tamoxifen and antidepressants – tamoxifen medication

  3. 5 Killer Quora Answers To Double Glazing Repairs Leeds Double glazing repairs leeds

  4. See What Small Bunk Bed For Kids Tricks The Celebs Are Using Small Bunk Bed For Kids (Doodleordie.Com)

  5. Nearby表示:

    The 3 Biggest Disasters In ADHD Diagnosis
    UK Private History Nearby

  6. The 10 Scariest Things About Waitrose Groceries Online Shopping Uk waitrose groceries online shopping uk

  7. What’s The Current Job Market For Window.Replacement Near Me Professionals Like?
    Window.replacement near Me

  8. Davidtes表示:

    buy cytotec pills online cheap buy cytotec online fast delivery buy cytotec over the counter

  9. firms表示:

    Why We Why We Injury Litigation (And You Should Also!) firms

  10. Davidtes表示:

    clomid nolvadex nolvadex gynecomastia tamoxifen alternatives premenopausal

  11. RandallTaulp表示:

    where can i buy cipro online: buy cipro online without prescription – buy cipro cheap

  12. Searching For Inspiration? Check Out Window Repair Near upvc window Repair near me

  13. WilliamRhype表示:

    https://nolvadex.life/# tamoxifen bone pain

  14. Go Porn Proxy表示:

    I think the admin of this web page is really working hard in support
    of his website, because here every data is quality based data.

  15. The 12 Worst Types Of Users You Follow On Twitter Double Glazed Units

  16. Ronaldpsymn表示:

    http://cytotec.club/# buy cytotec over the counter

  17. WilliamRhype表示:

    http://cytotec.club/# Misoprostol 200 mg buy online

  18. See What Online Charity Shop Uk Clothes Tricks The Celebs
    Are Making Use Of online charity shop uk clothes

  19. What’s The Current Job Market For Double Glazed Window Repairs Professionals Like?

    double glazed window repairs

  20. The History Of Auto Accident Case In 10 Milestones auto Accident law firm, http://www.encoskr.com,

  21. Five Killer Quora Answers To Fridge Freezer For Sale fridge freezer For Sale

  22. Davidtes表示:

    Cytotec 200mcg price cytotec pills buy online cytotec online

  23. Repaired表示:

    How To Outsmart Your Boss On Repairs To Upvc Windows Repaired

  24. Your means of describing the whole thing in this article is genuinely fastidious, all
    can simply be aware of it, Thanks a lot https://drive.google.com/file/d/1fSRRs-AIlIuvWWSkHXvgWTZh9qkJUNlc/view

  25. Margarette表示:

    The 10 Scariest Things About Childrens Double Bunk Bed quadruple beds – Margarette,

  26. Ronaldpsymn表示:

    http://lisinopril.network/# lisinopril from canada

  27. RandallTaulp表示:

    tamoxifen cost: how to lose weight on tamoxifen – nolvadex vs clomid

  28. The Most Valuable Advice You Can Receive About Designer Handbags Black designer handbags london

發佈留言

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