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,476,999 Responses

  1. Hi there to every , since I am in fact eager of reading this website’s
    post to be updated daily. It consists of good data.

  2. Love the insights shared here about checking credentials—very useful advice Smart Gas Solutions for Heating System Efficiency Upgrades

  3. Audi A3 Keys表示:

    This Is How Audi A3 Replacement Key Will Look Like In 10 Years
    Time Audi A3 Keys

  4. Erik Powers表示:

    Thanks for the valuable article. More at commercial scaffolding

  5. Fantastic article at the importance of drive washing! It truly revitalizes backyard areas. For extra insights, discuss with affordable commercial washing San Antonio

  6. 10 Life Lessons We Can Learn From Fridge Freezer 50/50
    Frost Free Small frost free Fridge freezer 50 50

  7. 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트

  8. I used to be able to find good advice from your articles.

  9. Good post. I learn something new and challenging on websites I stumbleupon on a daily basis.
    It’s always helpful to read articles from other writers and use something from other web
    sites.

  10. неотложная наркологическая помощь в москве неотложная наркологическая помощь в москве .

  11. Carl Blair表示:

    Pour ceux qui hésitent encore: plongez-vous ici et découvrez tout ce que vous devez meilleurs serrurier-lyonnais

  12. Fantastic overview of common mistakes in digital marketing—it could be helpful to consult an expert team from best seo company

  13. THC products表示:

    I pay a quick visit daily a few blogs and information sites to read posts, except this weblog provides quality based writing.

  14. gangbang表示:

    Thanks for one’s marvelous posting! I seriously enjoyed reading it, you can be a great author.I will make sure to bookmark your blog and
    will eventually come back down the road. I want to encourage yourself to continue your great work, have a nice day!

  15. Donaldabula表示:

    Cialis without a doctor prescription MaxPillsForMen.com Buy Tadalafil 10mg

  16. I have been surfing online greater than three hours as of late, but I
    never found any attention-grabbing article like yours.

    It is beautiful value sufficient for me.
    In my opinion, if all web owners and bloggers made excellent content as you did, the net will probably be a lot more useful than ever before.

  17. Is Your Company Responsible For An Fridge Freezer LG Budget?
    12 Top Ways To Spend Your Money lg fridge freezer black

發佈留言

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