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,232,296 Responses

  1. 10 Methods To Build Your Window Repair Leeds Empire Door repairs leeds

  2. DavidOwelo表示:

    http://kamagra.win/# Kamagra tablets

  3. 14 Clever Ways To Spend Leftover Double Glazed Window Replacement Near Me Budget double glazing panels

  4. CharlesDog表示:

    http://kamagra.win/# buy kamagra online usa

  5. CharlesDog表示:

    http://levitrav.store/# Cheap Levitra online

  6. Three Reasons To Identify Why Your Cheap Couches For Sale Isn’t Working
    (And How To Fix It) Sleeper Sofas for sale

  7. DavidOwelo表示:

    http://kamagra.win/# buy kamagra online usa

  8. 9 . What Your Parents Teach You About Titration ADHD Medications Titration Adhd Medications

  9. Lavon表示:

    10 Things Everybody Hates About Marc Jacobs Crossbody
    Bag marc jacobs tote bag sale, Lavon,

  10. 5 Laws That Will Help The Robot Vacuum And Mop Industry top 10 Robot vacuums

  11. The Story Behind Bunk Beds Can Haunt You Forever! farmhouse Bunk Beds

  12. fantastic publish, very informative. I ponder why the
    opposite experts of this sector don’t notice this. You should continue
    your writing. I’m confident, you have a huge readers’ base already!

    Also visit my blog; Energy Efficient Lighting

  13. Five Killer Quora Answers On Double Glazing Repairs Leeds double glazing repairs leeds

  14. 15 Top Online Shopping Sites Bloggers You Should Follow Integrated Led Fan

  15. The Reason Everyone Is Talking About Double Glazed Window Repair Today Window Repairs Near Me

  16. CharlesDog表示:

    https://cialist.pro/# Tadalafil Tablet

  17. Ten Coffee Bean Machines That Really Change Your Life Coffee Bean Machines

  18. Ten Apps To Help Control Your Repair Double Glazing upvc Door repairs near me

  19. CharlesDog表示:

    https://cialist.pro/# Buy Tadalafil 10mg

  20. JamesNow表示:

    cheapest cenforce: Cenforce 100mg tablets for sale – cenforce.pro

  21. Why Double Glazing Repair Kit Isn’t A Topic That People Are Interested In. Window Doctor Near Me

  22. 7 Practical Tips For Making The Most Of Your Three Wheel Mobility Scooter 3 wheeled mobility scooters

  23. Nine Things That Your Parent Taught You About
    Large Sectional Sofa Sectional Sofa

  24. CharlesDog表示:

    https://levitrav.store/# Buy Vardenafil 20mg

  25. 9 Things Your Parents Teach You About Fiat Panda Key Fob Replacement fiat Panda key

  26. Guide To Cleaning Robot Mop And Vacuum: The Intermediate Guide On Cleaning Robot Mop And Vacuum cleaning robot mop and vacuum

  27. JamesNow表示:

    Kamagra Oral Jelly: kamagra.win – Kamagra 100mg

  28. 9 . What Your Parents Teach You About Mazda 2 Spare Key mazda 2 Spare Key

  29. GeorgeSep表示:

    cenforce.pro: cheapest cenforce – Buy Cenforce 100mg Online

  30. CharlesDog表示:

    http://levitrav.store/# Buy Vardenafil 20mg

發佈留言

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