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,880 Responses

  1. Cyrus表示:

    Where Is Coffee Machines For Pods 1 Year From What Is Happening Now?
    coffee pod machine (Cyrus)

  2. Five Killer Quora Answers On 3 Wheel Running Stroller 3 wheel running stroller

  3. LarryCoell表示:

    ultrabet bonus ultrabet yeni giris 1125 ultrabet giris

  4. LarryCoell表示:

    ultrabet bonus ultrabet ultrabet yeni giris 1125

  5. Hey! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy reading
    your blog posts. Can you recommend any other blogs/websites/forums that deal with the same topics?
    Thank you so much!

  6. Derekfar表示:

    ultrabet tr online: ultrabet giris – ultrabet yeni giris 1125

  7. стоимость согласования перепланировки квартиры в москве стоимость согласования перепланировки квартиры в москве .

  8. Derekfar表示:

    ultrabet guncel: ultrabet tr online – ultrabet tr online

  9. LarryCoell表示:

    slot oyunlar? puf noktalar? slot tr online slot oyunlar? puf noktalar?

  10. согласование перепланировки квартиры в москве под ключ stoimost-soglasovaniya-pereplanirovki-kvartiry.ru .

  11. 13 Things About Fireplace Surrounds You May Not Have Known Marble Fireplaces

  12. 5. Best Accident Attorneys Near Me Projects For Any Budget Lawyer near me accident

  13. согласование перепланировки квартиры в москве под ключ http://stoimost-soglasovaniya-pereplanirovki-kvartiry.ru/ .

  14. The 10 Most Terrifying Things About 1kg Coffee Beans Uk 1kg coffee beans

  15. Upvc Door Locks Eastleigh Tools To Help You Manage Your Daily
    Life Upvc Door Locks Eastleigh Trick That Everybody Should
    Be Able To Upvc Door Locks Eastleigh (Timeoftheworld.Date)

  16. Derekfar表示:

    en cok kazand?ran slot oyunlar?: slot tr online – az parayla cok kazandiran slot oyunlar?

  17. Virginia表示:

    A An Overview Of Private ADHD Test From Start To Finish private adult adhd assessment scotland (Virginia)

  18. LarryCoell表示:

    Casino Siteleri Casino Siteleri Deneme Bonusu Veren Siteler

  19. pornnerve.com表示:

    Hi to every body, it’s my first visit of this weblog; this blog consists of amazing and actually fine stuff for visitors.

發佈留言

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