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,376,823 Responses

  1. Tiffiny表示:

    Electric Fireplace Wall Mounted Isn’t As Tough As You Think electric fireplaces wall
    mounted (Tiffiny)

  2. Normally I do not read post on blogs, but I wish to say that this write-up very pressured
    me to check out and do so! Your writing style has been amazed me.
    Thank you, very nice post.

  3. BarryDof表示:

    https://rybelsus.shop/# rybelsus pill

  4. Jeremypag表示:

    buy semaglutide online buy semaglutide online semaglutide online

  5. LeroyAcuff表示:

    Профессиональный сервисный центр сервисный центр телефонов ремонт телефонов

  6. Xavier Monds表示:

    You’ll Be Unable To Guess Best Leather Sofa’s Secrets best leather sofa (Thelma)

  7. RamonRek表示:

    ozempic generic: ozempic coupon – ozempic

  8. 10 Sites To Help Learn To Be An Expert In Audi A4 Car Key Replacement
    Audi advance key

  9. I’m really impressed with your writing skills as well as with the layout on your blog.
    Is this a paid theme or did you modify it yourself?

    Anyway keep up the excellent quality writing, it is rare to see a nice blog
    like this one today.

  10. autospin777表示:

    It’s an awesome piece of writing in support of all the
    online people; they will take benefit from it I
    am sure.

  11. В магазине сейфов предлагают где купить сейф стоимость сейфа

  12. 5 Tools Everyone Involved In Double Glazed Sash Window Industry Should Be Utilizing Sash Windows

  13. Five Killer Quora Answers On Situs Terpercaya Situs Terpercaya,https://Social4Geek.Com/,

  14. Vickie Sloane表示:

    30 Inspirational Quotes About Social Anxiety Symptoms Anxiety breathing symptoms

  15. I am really thankful to the holder of this
    web site who has shared this fantastic post at at this time.

  16. Frankevild表示:

    https://ozempic.art/# ozempic coupon

  17. Frankevild表示:

    https://ozempic.art/# ozempic cost

  18. It’s an awesome article for all the web viewers; they will obtain benefit from
    it I am sure.

  19. Doyle Fain表示:

    20 Things Only The Most Devoted ADHD Assessment UK Private Fans Are Aware Of private Adhd assessment somerset

  20. Frankevild表示:

    https://ozempic.art/# ozempic

  21. Trent Fleck表示:

    The 10 Most Terrifying Things About Link Togel link togel (Vernell)

  22. 3KAUTO表示:

    Genuinely no matter if someone doesn’t understand afterward its up to
    other visitors that they will assist, so here it occurs.

  23. BarryDof表示:

    https://rybelsus.shop/# semaglutide cost

  24. 15 Amazing Facts About Three Wheel Pushchair You’ve Never Seen three Wheel Pushchairs

  25. Your method of telling everything in this piece of writing is genuinely good, every
    one be able to without difficulty be aware of it,
    Thanks a lot.

  26. Riley Niland表示:

    You’ll Never Guess This Small Sectional Sofa With Chaise’s Tricks Small Sectional Sofa With Chaise

  27. вывод из запоя капельница ростов вывод из запоя капельница ростов .

發佈留言

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