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,237,799 Responses

  1. Ronaldpsymn表示:

    https://lisinopril.network/# lisinopril tabs 4mg

  2. 10 Key Factors Regarding Coffee Machine With Pods You Didn’t Learn At School Office Coffee machines

  3. AndrewNuh表示:

    наркологический центр в алматы https://someblog.ru/

  4. A How-To Guide For Automobile Locksmith From Beginning To End Automobile Locksmiths

  5. RandallTaulp表示:

    prinivil price: medication lisinopril 5 mg – lisinopril 1 mg

  6. You actually reported that very well!

  7. 10 Easy Steps To Start Your Own Personal Injury Settlement Business personal injury lawsuit

  8. See What Link Alternatif Gotogel Tricks The Celebs Are Using link alternatif Gotogel

  9. 15 Reasons Not To Ignore Work From Home Jobs Online Uk Work from home jobs no experience uk

  10. window表示:

    20 Trailblazers Are Leading The Way In Double Glazing Fitters Near Me window

  11. RandallTaulp表示:

    cost cheap propecia prices: cost of propecia pill – buy propecia for sale

  12. RandallTaulp表示:

    cipro online no prescription in the usa: cipro 500mg best prices – ciprofloxacin order online

  13. You’ll Never Guess This Window Repair Near Me’s Tricks window repair near me

  14. Responsible For A Personal Injury Firm Near Me Budget?
    12 Top Ways To Spend Your Money personal injury lawyers houston

  15. You need to bbе a paгt of a contest for one
    of the most useful blogs оn the web. I most cerfainly wiⅼl recommend this web site!

    my blog post :: harga pagar panel beton

  16. RandallTaulp表示:

    ciprofloxacin mail online: buy cipro – buy ciprofloxacin over the counter

  17. WilliamRhype表示:

    https://lisinopril.network/# lisinopril otc

  18. Ronaldpsymn表示:

    https://finasteride.store/# cost of generic propecia without insurance

  19. RandallTaulp表示:

    lisinopril 5 mg tabs: where can i buy zestril – lisinopril price in india

  20. RandallTaulp表示:

    cytotec online: cytotec online – order cytotec online

  21. WilliamRhype表示:

    https://finasteride.store/# get propecia tablets

  22. 10 Things Everyone Hates About Counter Strike
    Global Offensive Ps4 cs2 case opening (http://www.Dermandar.com)

  23. Lost My Car Keys No Spare Tools To Make Your Everyday Lifethe Only Lost My Car Keys No Spare Trick That Every Person Must Know Lost My Car Keys No Spare, https://Www.Cheaperseeker.Com,

  24. Ten Reasons To Hate People Who Can’t Be Disproved Best Slot Payouts best slot machines

  25. RandallTaulp表示:

    buy cytotec pills online cheap: buy cytotec over the counter – order cytotec online

  26. WOW just what I was looking for. Came here
    by searching for Deck Builder

  27. WilliamRhype表示:

    https://nolvadex.life/# tamoxifen and bone density

  28. Clarita表示:

    Five Killer Quora Answers To SEO Services Company London seo services company london – Clarita,

  29. RandallTaulp表示:

    lisinopril india: lisinopril brand name in india – lisinopril 20 mg tablet price

發佈留言

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