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,232,058 Responses

  1. The Top Citroen Dispatch Remote Key Replacement The Gurus Are
    Using 3 Things citroen C3 picasso key fob

  2. ClydePab表示:

    ultrabet tr online: ultrabet tr online – ultrabet

  3. CarrollUnton表示:

    deneme bonusu veren siteler betturkey betturkey.com http://ultrabet-tr.online/# ultrabet bonus

  4. Are You Responsible For The Bi-Fold Door Hinges Budget?
    10 Unfortunate Ways To Spend Your Money tilt and turn hinges for upvc windows

  5. ArnHesia表示:

    Exceptional prices allow you to stromectol pill , you can buy medication from home.

  6. 匿名訪客表示:

    Hello There. I found your blog using msn. This is a very well written article.
    I’ll be sure to bookmark it and return to read more of your useful info.
    Thanks for the post. I’ll certainly comeback.

  7. kingranks.com表示:

    Which Website To Research Case Battle Online case battle fun (kingranks.com)

  8. pg slot表示:

    Woah! I’m really loving the template/theme of
    this site. It’s simple, yet effective. A lot of times it’s tough to get that “perfect balance”
    between user friendliness and visual appearance.
    I must say you’ve done a fantastic job with this. Additionally, the blog loads extremely fast for
    me on Safari. Exceptional Blog!

  9. Hi just wanted to give you a brief heads up and let you know a
    few of the images aren’t loading correctly. I’m not sure why but I think
    its a linking issue. I’ve tried it in two different web browsers and both show
    the same results.

    My page … เว็บไซต์คาสิโน ต่างประเทศ

  10. Why You’ll Definitely Want To Find Out More About
    Sash Window Repair sash window repair near me – armyglider0.bravejournal.net,

  11. Learn More About Private ADHD Diagnosis While Working From Home
    private Adhd assessment warwickshire (bookmarkinglive.com)

  12. LarryCoell表示:

    Canl? Casino Siteleri Canl? Casino Siteleri Deneme Bonusu Veren Siteler

  13. 5 Killer Quora Answers On Accident Lawyers Near Me accident Lawyers Near me

  14. Тут можно преобрести оружейные сейфы в москве оружейный шкаф купить

  15. LarryCoell表示:

    ultrabet yeni giris 1125 ultrabet giris ultrabet

  16. 10 Myths Your Boss Has Concerning Car Keys Cutting Near Me Keys cut and programmed near Me

  17. Hi, I want to subscribe for this web site to obtain most up-to-date updates, thus where can i do it please help.

  18. Guide To 1kg Roasted Coffee Beans: The Intermediate Guide For 1kg
    Roasted Coffee Beans 1kg roasted coffee beans

  19. LarryCoell表示:

    matadorbet.bid matadorbet.bid matadorbet.bid

  20. holzpool表示:

    Hey There. I found your blog using msn. This is a really well written article.
    I will be sure to bookmark it and come back to read more of your useful
    info. Thanks for the post. I will definitely comeback.

  21. sell A laptop表示:

    Very great post. I just stumbled upon your weblog and wanted to mention that I’ve ttruly loved browsing your weblog posts.
    After alll I will be subscribing ffor your feed and I am hoping you write once more soon!

    Also visit my homepage; sell A laptop

  22. Your Family Will Be Grateful For Getting This Asbestos Attorneys Mesothelioma Lawsuit

  23. Your means of explaining everything in this post is genuinely
    fastidious, every one be able to simply know it, Thanks a lot https://carryu.com/

  24. fobs表示:

    24 Hours For Improving Locksmith Near Me For Cars
    fobs

發佈留言

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