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,674,238 Responses

  1. Real-time workout monitoring guides your workouts.

    Here is my web page – fitness equipment southern california

  2. Davidtes表示:

    buy cipro online ciprofloxacin generic price ciprofloxacin order online

  3. Veta表示:

    Ten 18 Wheeler Accident Lawyers That Really Change Your Life 18 wheeler accident lawyers (Veta)

  4. A Full Gym Experience Anywhere, Whenever.

    Also visit my site: fitness equipment stores

  5. WilliamRhype表示:

    https://finasteride.store/# buy propecia without rx

  6. Real-time workout tracking guides your exercises.

    Also visit my web-site … fitness equipment stores

  7. Rosaline表示:

    8 Tips To Increase Your What CSGO Case Has The Best Odds Game prisma case (Rosaline)

  8. the specifications of various different heights エロ 人形(usually comparing 6 or 8 different heights), that website is most likely an Aliexpress/Alibaba seller.

  9. Aleida表示:

    Three Common Reasons Your Auto Lock Smiths Isn’t Working (And What You Can Do To
    Fix It) auto locksmith car key replacement, Aleida,

  10. What The Heck What Is Erb’s Palsy Litigation? Erb’s Palsy Lawsuits

  11. 30 Inspirational Quotes About Mesothelioma Lawsuit Mesothelioma settlement

  12. Davidtes表示:

    purchase cytotec buy cytotec in usa cytotec online

  13. The 10 Most Scariest Things About Treatments For Adhd In Adults treatments For adhd in adults, http://www.redsea.gov.eg,

  14. Real-time exercise monitoring guides your exercises.

    Here is my blog post – fitness equipment for sale

  15. Bookmarks表示:

    A Complete Fitness Center Experience Anywhere, At Any Moment.

    my blog post :: Bookmarks

  16. Davidtes表示:

    tamoxifen headache cost of tamoxifen does tamoxifen cause menopause

  17. asbestos law表示:

    The Most Successful Asbestos Settlement Gurus Do 3 Things asbestos law

  18. Bookmarks表示:

    A Complete Fitness Center Experience Anywhere, At Any Time.

    my homepage Bookmarks

  19. RandallTaulp表示:

    cytotec abortion pill: buy misoprostol over the counter – order cytotec online

  20. Grazie all’utilizzo di strumenti specifici e all’adozione di una corretta etica
    online, sarà possibile trovare la soluzione giusta per soddisfare i propri
    bisogni in totale sicurezza e rispetto della privacy altrui.

    Here is my web page – https://igram.world/it/story-saver

  21. Real-time exercise monitoring guides your workouts.

    Look at my page: mike's fitness equipment riverside california

  22. I think the admin of this web page is actually working hard for his web
    site, as here every material is quality based information.

  23. zapakita01表示:

    I’m really loving the theme/design of your site. Do you ever run into
    any browser compatibility problems? A few of my blog audience have complained about my site not working correctly in Explorer but
    looks great in Chrome. Do you have any suggestions to help fix this issue?

    my web-site :: zapakita01

  24. RandallTaulp表示:

    alternatives to tamoxifen: tamoxifen lawsuit – tamoxifen hair loss

  25. WilliamRhype表示:

    http://nolvadex.life/# nolvadex d

  26. Real-time workout tracking overviews your exercises.

    My homepage – fitness equipment in amarillo texas

  27. 9 Things Your Parents Taught You About Dryer Washer Combo
    washing machines Washer

發佈回覆給「fitness equipment in amarillo texas」的留言 取消回覆

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