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,432,455 Responses

  1. Melvinpah表示:

    Обработка квартир после пожара https://spec-uborka-posle-pozhara.ru/

  2. 10 Things You Learned In Kindergarden To Help You Get
    Started With All Terrain Prams pushchair for rough terrain

  3. Jorgeagopy表示:

    supplements for ed: cheap prescription drugs – ed drug prices

  4. LeroyBak表示:

    cure for ed low cost pharmacy ed pumps

  5. Delores表示:

    Think You’re Ready To Start Do You Need A Driving License For A Scooter?
    Take This Quiz testynaprawojazdy eu (Delores)

  6. 10 Wrong Answers To Common Asbestos Attorney Questions: Do You Know The
    Right Answers? Mesothelioma Lawyer

  7. Williamagric表示:

    new ed treatments http://indianpharm24.pro/# top 10 pharmacies in india

  8. LeroyBak表示:

    mexican mail order pharmacies mexico pharmacy cheap reputable mexican pharmacies online

  9. Cerys表示:

    10 Reasons That People Are Hateful To Leather Chesterfield Leather Chesterfield material chesterfield sofa (Cerys)

  10. Williamagric表示:

    discount prescription drugs http://mexicanpharm24.cheap/# mexican border pharmacies shipping to usa

  11. Why Truck Accident Lawyers Is So Helpful During COVID-19 truck accident attorneys

  12. Diplomi_qqoi表示:

    купить аттестат за 8 классов prema-diploms.ru .

  13. Diplomi_akoi表示:

    диплом о высшем образовании с занесением в реестр стоимость prema-diploms.ru .

  14. Lavazza Coffee Beans 1kg Techniques To Simplify Your Daily Lifethe One Lavazza Coffee Beans 1kg Trick
    That Everyone Should Know coffee Beans 1kg

  15. Need Inspiration? Check Out Programming A Car Key Mobile Car key programming near me

  16. JamesReubs表示:

    Надежный поставщик недорогой строительной арматуры. Качественная арматурная сталь а500с по выгодной цене с доставкой куплю арматуру

  17. DevonPhack表示:

    https://mexicanpharm24.cheap/# mexican drugstore online
    non prescription ed pills

  18. LeroyBak表示:

    indian pharmacy paypal Indian pharmacy online Online medicine home delivery

  19. 25 Shocking Facts About Best Cot Newborn baby cots online

  20. The 3 Most Significant Disasters In Car Keys Programming History Key Reprogramming

  21. DevonPhack表示:

    https://pharm24.pro/# best erection pills
    generic ed pills

  22. Jorgeagopy表示:

    herbal remedies for ed: cheap pharmacy online – prescription drugs online without

  23. Jorgeagopy表示:

    male enhancement: cheap pharmacy – buy ed pills

  24. Ingrid表示:

    10 Things Everybody Hates About Wall Mounted Electric Fires log effect wall mounted electric
    fires – Ingrid,

  25. habib表示:

    Hi, I read your blog like every week. Your writing style is witty,
    keep up the good work!

  26. DevonPhack表示:

    http://indianpharm24.pro/# indian pharmacy online
    medications for

  27. Williamagric表示:

    pet antibiotics without vet prescription https://indianpharm24.pro/# best online pharmacy india

發佈留言

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