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,664,776 Responses

  1. Max Lambert表示:

    Thanks for the clear advice. More at beauty parlour near me

  2. Bessie Love表示:

    Went into ### anykeyword ### today they usually were wonderful friendly!! Thank you all such a Best phone repair shop

  3. Drug information sheet. What side effects can this medication cause?
    get cheap zithromax no prescription
    Some news about pills. Read information here.

  4. Todd Santos表示:

    The art of floral setup is absolutely an expression of imagination! I ‘d love to find out more concerning it– where can I locate ideas? See me at local florist delivery for

  5. What’s up it’s me, I am also visiting this site regularly, this website is
    genuinely nice and the users are actually sharing good thoughts. http://secretour.xyz/bbs/board.php?bo_table=free&wr_id=179348

  6. 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트
    카지노사이트 카지노사이트

  7. BrettTIT表示:

    where to buy erectile dysfunction pills: cheap cialis – buying ed pills online

  8. Where Will Remote Key Fob Repair One Year From Today?
    Remote Key Fob Repairs

  9. Barrybib表示:

    where to get ed pills: fast pills easy – cheap ed

  10. essentially just a archive of transaction data, often referred to as a distributed accounting, which is duplicated and broadcast to a network of https://https://gamestop-wallet.io// users who are ready to control and coordinate data databases.

  11. Drugs information. Drug Class.
    where can i buy cheap phenergan prices
    Some what you want to know about medication. Get information here.

  12. For latest news you have to visit web and on the web I found this web page as a best website for
    most up-to-date updates.

  13. Donaldabula表示:

    Generic Tadalafil 20mg price MaxPillsForMen buy cialis pill

  14. Mighty Dog Roofing
    Reimer Druve North 13768
    Maple Grove, MN 55311 Unitesd Ꮪtates
    (763) 280-5115
    seamless siding replacements

  15. I found this very interesting. For more, visit Cypress Pro Wash – pressure washing company

  16. 5 Laws Anybody Working In Peritoneal Mesothelioma
    Asbestos Should Be Aware Of Mesothelioma Attorneys

  17. BrettTIT表示:

    viagra without prescription: FastPillsForMen.com – over the counter sildenafil

  18. 15 Gifts For The Shipping Containers Lover In Your Life 20ft shipping Containers

  19. Charlesbot表示:

    https://maxpillsformen.com/# Buy Cialis online

  20. This was a great help. Check out access platforms for more

  21. This was highly helpful. For more, visit scaffolding

  22. вывод из запоя цены ростов-на-дону вывод из запоя цены ростов-на-дону .

  23. выезд на дом капельница от запоя выезд на дом капельница от запоя .

  24. Hello, i read your blog from time to time and i own a similar one and
    i was just curious if you get a lot of spam comments?
    If so how do you protect against it, any plugin or anything
    you can advise? I get so much lately it’s driving me
    crazy so any support is very much appreciated.

  25. Charlesbot表示:

    https://fastpillsformen.com/# best price for viagra 100mg

發佈回覆給「Bessie Love」的留言 取消回覆

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