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,672,997 Responses

  1. Also visit my web site – Avto.izmail.Es

  2. ImmediateX表示:

    It’s going to be end of mine day, except before finish I am reading this impressive paragraph
    to increase my knowledge.

  3. online casino表示:

    porn cannibalism

  4. BrandenSlomy表示:

    dapoxetine online: buy priligy max pharm – cheap priligy

  5. Porn表示:

    all the time i used to read smaller articles which as well clear their motive, and that is also happening with
    this paragraph which I am reading at this time.

  6. Тут можно преобрести сейфы огнестойкие огнеупорные сейфы

  7. Dereketerm表示:

    prednisone 10mg tablet cost: buy prednisone – order prednisone 10mg

  8. ishikawamio表示:

    Pretty nice post. I just stumbled upon your blog and
    wanted to say that I’ve really enjoyed browsing your blog posts.
    In any case I will be subscribing to your feed and I hope you write again very soon!

  9. Danieljek表示:

    buying amoxicillin online http://prednisoneraypharm.com/# prednisone 5 mg tablet price

  10. StevenNub表示:

    buy priligy max pharm: max pharm – cheap priligy

  11. Medicament information. Short-Term Effects.
    does famotidine contain aluminum or magnesium
    All news about drugs. Get here.

  12. Danieljek表示:

    how to get amoxicillin over the counter https://priligymaxpharm.com/# buy priligy max pharm

  13. Nice post. I was checking constantly this blog and I’m impressed!
    Very useful info specifically the last part 🙂 I care
    for such information much. I was looking for this particular
    info for a long time. Thank you and best of luck.

  14. Salesforce表示:

    Salesforce Commerce Cloud est une plateforme robuste qui vous offre des outils intuitifs pour créer et gérer des boutiques en ligne intégrant des fonctionnalités avancées telles que le marketing numérique et les médias sociaux.

  15. Your mode of telling all in this article is in fact nice,
    all be able to easily know it, Thanks a lot https://logosportqc.com/

  16. Bradleygoath表示:

    buy priligy max pharm buy priligy max pharm priligy

  17. Hey There. I found your blog using msn. That is a very well written article.
    I will be sure to bookmark it and return to learn more of your useful info.
    Thanks for the post. I will definitely return.

  18. Danieljek表示:

    amoxicillin buy canada http://amoxilcompharm.com/# can you buy amoxicillin over the counter

  19. Bradleygoath表示:

    where can i get cheap clomid no prescription clomid how can i get clomid without insurance

發佈留言

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