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,414,428 Responses

  1. Bookmarks表示:

    Brandbusters TShirt Layout Project Submission.

    Also visit my web-site – Bookmarks

  2. HeathZex表示:

    Quand une femme prend du Viagra homme: viagra sans ordonnance – Viagra Pfizer sans ordonnance

  3. They have items to market or services to use.

    Here is my site: Can a foundation apply for Grants

  4. Real HGH Rewards (NewULife Hgh Gel Not Checked Yet).

    Feel free to visit my website :: vigabatrin 500mg brand name

  5. Genuine HGH Perks (NewULife Hgh Gel Not Examined Yet).

    Visit my web site nib makeup foundation

  6. Your way of telling the whole thing in this
    article is genuinely pleasant, all be able to easily understand it, Thanks a lot https://boutiquelemarie.com/comment-combiner-les-chemises-et-les-cravates/

  7. Stephengen表示:

    https://clssansordonnance.icu/# pharmacie en ligne sans ordonnance

  8. A synthetic USP human growth hormonal agent (somatropin).

    my blog … Is foundation necessary in makeup

  9. 60+ sensational instances of geometric layouts.

    My blog post :: t shirt design website free

  10. HeathZex表示:

    Viagra homme prix en pharmacie sans ordonnance: Sildenafil Viagra – Acheter viagra en ligne livraison 24h

  11. Genuine HGH Perks (NewULife Hgh Gel Not Evaluated Yet).

    my page – is foundation necessary in makeup

  12. Bookmarks表示:

    Typeography TShirt Layout Example Bootstrapping.

    my web-site … Bookmarks

  13. HeathZex表示:

    Viagra gГ©nГ©rique pas cher livraison rapide: Viagra generique en pharmacie – Viagra sans ordonnance 24h Amazon

  14. What a stuff of un-ambiguity and preserveness of valuable experience
    about unexpected feelings.

    My web page; provadent a scam

  15. HeathZex表示:

    п»їpharmacie en ligne france: pharmacie en ligne france livraison belgique – pharmacie en ligne france pas cher

  16. Charlesrab表示:

    Prix du Viagra 100mg en France Viagra sans ordonnance 24h Meilleur Viagra sans ordonnance 24h

  17. Bookmarks表示:

    Individuals put on t-shirts to do everything: Stroll the canine.

    Feel free to visit my blog post – Bookmarks

  18. NewULife Ranks. Invite to my NewUlife Evaluation.

    Feel free to visit my blog post Nib meaning in construction

  19. Bookmarks表示:

    In this case, the life insurance policy firm’s money.

    Feel free to visit my website – Bookmarks

  20. Bookmarks表示:

    NewULife Ranks. Welcome to my NewUlife Evaluation.

    Here is my web-site; Bookmarks

  21. Просто торговля на интернет фишки рано или поздно перерастет в
    проект на монеты и купюры, а вот перерастет ли игра со ставками в.

    My web-site – https://rupokerredstar.ru/

  22. Typeography TShirt Style Example Bootstrapping.

    Review my homepage: t shirt design ideas pinterest

  23. Bookmarks表示:

    What is New U Life SOMADERM Transdermal Gel.

    Feel free to surf to my blog Bookmarks

  24. 60+ stunning instances of geometric styles.

    My web site … t Shirt design app for android

  25. HeathZex表示:

    pharmacie en ligne france fiable: Acheter Cialis 20 mg pas cher – trouver un mГ©dicament en pharmacie

  26. Howdy! I simply wish to give you a big thumbs up for your great information you’ve got right here on this post.
    I am coming back to your website for more soon.

  27. Good day! This is my 1st comment here so I just wanted to give a quick shout out and tell you I
    genuinely enjoy reading through your posts.
    Can you suggest any other blogs/websites/forums that
    deal with the same topics? Thanks a lot!

發佈留言

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