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,208 Responses

  1. What Freud Can Teach Us About Marc Jacobs Handbag Black marc jacobs tote bag small

  2. Davidtes表示:

    buy generic propecia without a prescription buy generic propecia cheap propecia without dr prescription

  3. RandallTaulp表示:

    ciprofloxacin generic price: buy cipro – ciprofloxacin generic price

  4. Davidtes表示:

    buy generic ciprofloxacin buy cipro cheap ciprofloxacin generic price

  5. See What Treadmill Home Gym Tricks The Celebs Are Using Treadmill home

  6. 8 Tips To Improve Your Veterans Disability Lawsuit Game Veterans disability Law Firm

  7. Davidtes表示:

    get generic propecia pill cost of propecia pills generic propecia no prescription

  8. Malorie表示:

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
    You clearly know what youre talking about, why throw away your
    intelligence on just posting videos to your site when you could be giving us something informative to read?

  9. Hildred表示:

    Nine Things That Your Parent Taught You About SEO Tools Search Engine Software seo tools search engine software (Hildred)

  10. Davidtes表示:

    cytotec pills buy online Abortion pills online buy cytotec over the counter

  11. RandallTaulp表示:

    ciprofloxacin generic price: cipro generic – buy cipro cheap

  12. Davidtes表示:

    tamoxifen cyp2d6 where to buy nolvadex tamoxifen for men

  13. RandallTaulp表示:

    tamoxifen depression: does tamoxifen make you tired – tamoxifen chemo

  14. Why All The Fuss About Avon Spray Skin So Soft? avon Spray skin so soft

  15. It’s in fact very complicated in this full of activity life to
    listen news on TV, thus I simply use web for that reason, and
    obtain the most recent information.

  16. web Case表示:

    7 Tips To Make The Most Out Of Your CSGO Cases Opening
    web Case

  17. The Three Greatest Moments In Double Glazed Units Manufacturers
    Near Me History Double Glazed Windows Repairs

  18. treadmill表示:

    10 Best Books On Tread Mills treadmill

  19. Eldon表示:

    9 Lessons Your Parents Taught You About Saab Key Programming Near Me saab
    key programming near me (Eldon)

  20. WilliamRhype表示:

    http://cytotec.club/# buy cytotec

  21. WilliamRhype表示:

    http://lisinopril.network/# prinivil coupon

  22. Ronaldpsymn表示:

    http://nolvadex.life/# tamoxifen and antidepressants

  23. Davidtes表示:

    buy propecia tablets buy cheap propecia tablets order generic propecia without dr prescription

  24. firms表示:

    Where Will Personal Injury Claim Be One Year From What Is Happening Now?

    firms

  25. Ronaldpsymn表示:

    http://ciprofloxacin.tech/# cipro ciprofloxacin

  26. RandallTaulp表示:

    propecia generics: cost of generic propecia – get cheap propecia tablets

  27. RandallTaulp表示:

    cipro: where can i buy cipro online – cipro

  28. Responsible For The Repairing Upvc Windows Budget? 12 Tips On How To Spend Your Money upvc Window Repair

  29. RandallTaulp表示:

    buy cheap propecia online: get cheap propecia without a prescription – buy propecia without prescription

  30. 10 Things That Your Family Taught You About Replace Window Handles Replace Window handles

發佈留言

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