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,211,561 Responses

  1. WayneCoirl表示:

    mexico drug stores pharmacies: mexican pharmaceuticals online – purple pharmacy mexico price list

  2. 17 Reasons Why You Shouldn’t Ignore Railroad Injuries Attorneys railroad injuries lawsuit

  3. Nelsonbluck表示:

    mexico drug stores pharmacies: mexico pharmacies prescription drugs – mexican drugstore online

  4. From Around The Web Twenty Amazing Infographics About Electric Wheelchairs Near Me Electric Wheel Chairs

  5. It’s The Perfect Time To Broaden Your Replacement Upvc Window Handles Options upvc window repair near me

  6. Arnoldten表示:

    purple pharmacy mexico price list best online pharmacies in mexico mexico pharmacy

  7. Nelsonbluck表示:

    mexico drug stores pharmacies: mexican online pharmacies prescription drugs – pharmacies in mexico that ship to usa

  8. Arnoldten表示:

    mexican border pharmacies shipping to usa mexico drug stores pharmacies medication from mexico pharmacy

  9. Craps – The Most Fun Carbohydrates Have At Any Casino 프라그마틱
    무료슬롯 (Harrahsresortsandcasinos.Net)

  10. Where Will Parent Facing Pushchair Be 1 Year From This Year?
    toddler Stroller

  11. Medicine information. Cautions.
    can you get norvasc price
    Actual what you want to know about medicine. Read now.

  12. Arnoldten表示:

    mexico drug stores pharmacies mexico drug stores pharmacies purple pharmacy mexico price list

  13. Nelsonbluck表示:

    mexico drug stores pharmacies: mexican mail order pharmacies – mexican pharmaceuticals online

  14. WayneCoirl表示:

    mexican mail order pharmacies: best online pharmacies in mexico – buying from online mexican pharmacy

  15. WayneCoirl表示:

    mexican drugstore online: mexican mail order pharmacies – medicine in mexico pharmacies

  16. StevenAdhep表示:

    Итоговая цена сертификата ИСО 9001 и сроки проведения работ всегда определяются в индивидуальном порядке. Их можно назвать, только когда заказчик предоставит всю необходимую информацию https://www.sostav.ru/blogs/30357/19844

  17. WayneCoirl表示:

    medicine in mexico pharmacies: п»їbest mexican online pharmacies – mexico pharmacies prescription drugs

  18. DominicJaw表示:

    reputable mexican pharmacies online: mexico drug stores pharmacies – mexican pharmaceuticals online

  19. You’ll Never Be Able To Figure Out This ADHD Diagnosis In Adults’s
    Tricks Adhd diagnosis in adults

  20. 15 Things Your Boss Wishes You Knew About Three Wheeler Pushchair 3 wheeled Pushchair

  21. 整骨表示:

    https://bogin98.z5.web.core.windows.net/research/bogin98-(18).html
    Discover our hand-picked assortment of mom of the bride attire and you’re assured to be best-dressed – aside from the bride, of course!

  22. WayneCoirl表示:

    reputable mexican pharmacies online: mexico drug stores pharmacies – mexico drug stores pharmacies

  23. DominicJaw表示:

    mexico drug stores pharmacies: п»їbest mexican online pharmacies – mexican border pharmacies shipping to usa

  24. Arnoldten表示:

    mexican pharmacy buying prescription drugs in mexico buying from online mexican pharmacy

  25. Nelsonbluck表示:

    buying prescription drugs in mexico online: mexican border pharmacies shipping to usa – mexican rx online

  26. Telegra.Ph表示:

    10 Sites To Help You Learn To Be An Expert In Mesothelioma Legal Mesothelioma Lawsuits (Telegra.Ph)

  27. A. The Most Common Used Mobility Scooters For Sale Debate Doesn’t Have To Be As Black And White As You Think Cheapest Mobility Scooters For Sale

發佈留言

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