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,195,511 Responses

  1. I am in fact thankful to the owner of this site who has shared this impressive
    piece of writing at at this time.

    Here is my webpage :: asbestos abatement ottawa

  2. asbestos表示:

    I read this article fully about the resemblance of most up-to-date and earlier technologies,
    it’s remarkable article.

    Here is my web blog :: asbestos

  3. Tips To Leave Of Your Card Debt Now! 중소기업 대출

  4. How To Choose The Right Double Glazed Front Doors Near Me On The Internet double Glased windows

  5. MichaelPayof表示:

    buying prescription drugs in mexico online
    https://cmqpharma.com/# mexico drug stores pharmacies
    mexican border pharmacies shipping to usa

  6. lgbt porn表示:

    incest porn

  7. MichaelPayof表示:

    buying prescription drugs in mexico
    http://cmqpharma.com/# mexico drug stores pharmacies
    mexico pharmacies prescription drugs

  8. Good article. I absolutely appreciate this website.
    Stick with it!

  9. Henrylip表示:

    medication from mexico pharmacy: mexican online pharmacy – medicine in mexico pharmacies

  10. Be On The Lookout For: How Medical Malpractice Litigation Is Taking Over And What Can We Do About It medical malpractice attorneys

  11. You’ll Never Guess This Double Glazed Replacement Windows’s
    Benefits Double Glazed Replacement Windows

  12. Piece of writing writing is also a fun, if you be acquainted with then you can write or else it is complicated to write.

  13. MichaelPayof表示:

    mexican mail order pharmacies
    http://cmqpharma.com/# п»їbest mexican online pharmacies
    reputable mexican pharmacies online

  14. Henrylip表示:

    best online pharmacies in mexico: mexican online pharmacy – mexican rx online

  15. MichaelPayof表示:

    medication from mexico pharmacy
    https://cmqpharma.online/# mexican mail order pharmacies
    best online pharmacies in mexico

  16. Bad Credit Bank Account 보증금 대출 (f.r.a.g.ra.nc.e.rnmn)

  17. Henrylip表示:

    buying prescription drugs in mexico online: cmqpharma.com – mexico pharmacy

  18. Henrylip表示:

    mexico drug stores pharmacies: online mexican pharmacy – best online pharmacies in mexico

  19. MichaelPayof表示:

    buying prescription drugs in mexico online
    https://cmqpharma.com/# mexican online pharmacies prescription drugs
    mexico drug stores pharmacies

  20. Amazing blog! Do you have any hints for aspiring writers?
    I’m hoping to start my own website soon but I’m a little lost on everything.
    Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many
    choices out there that I’m totally confused .. Any recommendations?
    Many thanks!

  21. MichaelPayof表示:

    mexican drugstore online
    https://cmqpharma.online/# medicine in mexico pharmacies
    mexican rx online

  22. Henrylip表示:

    mexico pharmacies prescription drugs: cmqpharma.com – pharmacies in mexico that ship to usa

  23. What’s up to all, how is everything, I think every one is getting more from this website,
    and your views are nice for new visitors.

    Here is my web-site … asbestos abatement ottawa

發佈留言

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