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,665,882 Responses

  1. Larrylab表示:

    indian pharmacy indian pharmacy pharmacy website india

  2. I’m really enjoying the website design billings and layout of your site.
    It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often.
    Did you hire out a developer to create your theme?
    Great work!

  3. Chasefounc表示:

    cheapest online pharmacy india: Healthcare and medicines from India – Online medicine home delivery

  4. Larrylab表示:

    mexico drug stores pharmacies mexican pharmacy mexican drugstore online

  5. Chasefounc表示:

    safe reliable canadian pharmacy: Canada pharmacy – online canadian pharmacy review

  6. LamarSix表示:

    top online pharmacy india: Generic Medicine India to USA – top 10 online pharmacy in india

  7. Pills information. Effects of Drug Abuse.
    where can i get amoxil
    Best about medicines. Read information now.

  8. DavidJeado表示:

    canadian pharmacy in canada: canadian pharmacy – pet meds without vet prescription canada

  9. Larrylab表示:

    mexican drugstore online mexican pharmacy medicine in mexico pharmacies

  10. LamarSix表示:

    pharmacy in canada: Pharmacies in Canada that ship to the US – recommended canadian pharmacies

  11. Lembu4d表示:

    Hi, its pleasant article regarding media print, we all be familiar with media is a great source of facts.

  12. Larrylab表示:

    online shopping pharmacy india best online pharmacy india indian pharmacy paypal

  13. Now I am ready to do my breakfast, afterward having my breakfast coming yet again to read
    other news.

  14. Greetings! Very useful advice within this post! It’s the little changes that make the most significant changes.
    Thanks a lot for sharing!

    Also visit my web page design website billings

  15. Aw, this was a really good post. Spending some time and actual effort
    to create a top notch article… but what can I say… I put things off a whole
    lot and never manage to get nearly anything done.

  16. Oven表示:

    It’s appropriate time to make some plans for the future and it’s time to be happy.
    I have read this post and if I could I wish to
    suggest you few interesting things or tips. Maybe you could
    write next articles referring to this article.

    I desire to read more things about it!

  17. Larrylab表示:

    mexico drug stores pharmacies mexican pharmacy buying prescription drugs in mexico

  18. I know this if off topic but I’m looking into starting
    my own weblog and was wondering what all is needed to get setup?

    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very internet savvy so I’m not 100% positive.
    Any recommendations or advice would be greatly appreciated.
    Thanks

    my web site: web design billings

  19. Pills information leaflet. Cautions.
    amoxil
    Some about pills. Get here.

  20. trading表示:

    Hello beneficial friend. Let me introduce myself. I am Tod
    Reep but I never really liked that name. His wife and him chose to exist in North Carolina.
    For years I’ve been working as being a production and planning company.
    What she loves doing is caving and she’s been doing it for an amazing while.

  21. Larrylab表示:

    safe canadian pharmacy Pharmacies in Canada that ship to the US canadian pharmacy oxycodone

  22. Roscoeevern表示:

    https://canadianpharmgrx.xyz/# best canadian pharmacy

  23. I’m not sure why but this blog is loading incredibly slow for
    me. Is anyone else having this issue or is it a problem on my end?
    I’ll check back later on and see if the problem still exists.

    Here is my site :: web design billings

  24. LamarSix表示:

    canadian drug: Cheapest drug prices Canada – reputable canadian online pharmacy

  25. LamarSix表示:

    mail order pharmacy india: Healthcare and medicines from India – buy prescription drugs from india

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

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