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,376,019 Responses

  1. ThelmaFange表示:

    On our website you will find unique collection rare coins from all over the world. We offer both modern and historical specimens that will become real finds for collectors.
    For more information, follow the link [url=https://numizmatexpert.ru/]юбилейные рубли ссср – каталог [/url]
    Our team of experts carefully selects each coin, ensuring authenticity.

  2. RonaldLit表示:

    найкращі фільми онлайн дивитися кіно онлайн

  3. RobertJar表示:

    amoxicillin 500mg tablets price in india: AmoxStar – amoxicillin online without prescription

  4. SamuelBix表示:

    can you buy amoxicillin uk: amoxicillin medicine – where to buy amoxicillin pharmacy

  5. Annie Moody表示:

    Never underestimate power knowledge brings forth when tackling potential hazards presented through Mother Nature herself attempting disrupt lives/homes frequently!!   ##AnyKeyWord### Water restoration

  6. KennethPah表示:

    order amoxicillin online Amox Star amoxicillin script

  7. DanielAbice表示:

    https://amoxstar.com/# azithromycin amoxicillin

  8. KennethPah表示:

    buy cipro without rx CiPharmDelivery buy ciprofloxacin

  9. Ten Things You Need To Be Aware Of Back Injury Law
    Firm Injury Attorney Near Me

  10. koitoto表示:

    I delight in, cause I found exactly what I was
    looking for. You’ve ended my 4 day long hunt! God Bless you man. Have
    a nice day. Bye

  11. Chase Craig表示:

    I found some great deals on moving services at westchester local moving companies – worth checking

  12. DanielAbice表示:

    https://cipharmdelivery.com/# buy cipro no rx

  13. Georgeglomb表示:

    онлайн фільм безкоштовно якості фільми 2024 дивитися онлайн

  14. cookies表示:

    I loved as much as you will receive carried out right here. The sketch is attractive, your authored material stylish. nonetheless, you command get bought an edginess over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly a lot often inside case you shield this increase.

  15. KennethPah表示:

    can i buy clomid for sale clomid on pharm order cheap clomid without prescription

  16. SamuelBix表示:

    buy cipro online usa: ci pharm delivery – buy cipro

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

    Feel free to surf to my homepage: 隱形眼鏡價格

  18. DanielAbice表示:

    http://amoxstar.com/# where can i buy amoxicillin over the counter uk

發佈留言

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