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...

911,094 Responses

  1. Thanks for sharing your thoughts on website design billings.
    Regards

  2. 1. Как выбрать идеальный гипсокартон для ремонта
    магазины строительных материалов москва купить гипсокартон .

  3. Larrylab表示:

    canadian pharmacy 24 Canada pharmacy canadian pharmacy

  4. If you are going for finest contents like me, only
    pay a visit this site all the time for the reason that it presents quality contents, thanks

  5. Roscoeevern表示:

    https://canadianpharmgrx.xyz/# canadian pharmacy world

  6. Larrylab表示:

    canadian pharmacy online reviews Canada pharmacy online canadianpharmacy com

  7. DavidJeado表示:

    canadian pharmacy tampa: Canada pharmacy online – canada pharmacy

  8. Hey There. I found your blog using msn. This is an extremely well written article.
    I will make sure to bookmark it and return to read more of your useful information.
    Thanks for the post. I’ll definitely comeback.

    my webpage design website billings

  9. I am curious to find out what blog platform you have been utilizing?

    I’m experiencing some minor security issues with my
    latest blog and I’d like to find something more safe.
    Do you have any recommendations?

  10. It’s perfect time to make some plans for the future and it’s time to
    be happy. I’ve read this post and if I could I desire to suggest you few interesting
    things or suggestions. Perhaps you could write next articles referring to this article.
    I desire to read more things about it!

  11. Larrylab表示:

    legitimate canadian pharmacy CIPA approved pharmacies northwest pharmacy canada

  12. Chasefounc表示:

    medication from mexico pharmacy: Pills from Mexican Pharmacy – mexican drugstore online

  13. DavidJeado表示:

    mexico drug stores pharmacies: Pills from Mexican Pharmacy – medicine in mexico pharmacies

  14. Roscoeevern表示:

    https://indianpharmgrx.com/# reputable indian pharmacies

  15. Larrylab表示:

    buying prescription drugs in mexico mexican pharmacy medicine in mexico pharmacies

  16. Larrylab表示:

    mexico drug stores pharmacies Pills from Mexican Pharmacy mexican online pharmacies prescription drugs

  17. cadplm.co.kr表示:

    Here is my web-site – kkpoker review (cadplm.co.kr)

  18. Larrylab表示:

    mexican mail order pharmacies Pills from Mexican Pharmacy mexican online pharmacies prescription drugs

  19. Roscoeevern表示:

    http://canadianpharmgrx.com/# buy drugs from canada

  20. Larrylab表示:

    online pharmacy india indian pharmacy п»їlegitimate online pharmacies india

  21. Touche. Outstanding arguments. Keep up the great work.

    My webpage :: design website billings

  22. DavidJeado表示:

    best online pharmacy india: indian pharmacy delivery – п»їlegitimate online pharmacies india

  23. Hey there, You’ve done a fantastic job. I will certainly digg it and personally recommend
    to my friends. I am confident they will be benefited from this web design billings
    site.

  24. Отличный сайт! Всем рекомендую, качественный и быстрый ремонт техники! ремонт iphone спб

  25. My developer is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the costs. But he’s tryiong none the less.
    I’ve been using WordPress on a variety of websites for about a year and am worried about switching to another platform.
    I have heard fantastic things about blogengine.net. Is there a way I can transfer
    all my wordpress posts into it? Any kind of help would be really appreciated!

  26. Какой призовой https://gagdetfrontal.com/ фонд покер фриролла?

  27. Chasefounc表示:

    is canadian pharmacy legit: canadian drug pharmacy – canada pharmacy world

發佈留言

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