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,236,040 Responses

  1. LarryCoell表示:

    Deneme Bonusu Veren Siteler guvenilir casino siteleri Canl? Casino Siteleri

  2. dede4d表示:

    Goօd article! We will be linkіng to this great content on our
    website. Keep up the great wгiting.

    My web site dede4d

  3. Can you tell us more about this? I’d love to find out some additional information.

  4. ClydePab表示:

    en kazancl? slot oyunlar?: az parayla cok kazandiran slot oyunlar? – slot oyunlar? puf noktalar?

  5. What’s The Current Job Market For Best Car Seat Newborn Professionals Like?
    car Seat newborn

  6. Тут можно преобрести сейф для оружие сейф сейфы оружейный оружие

  7. проект перепланировки квартиры цена москва http://www.stoimost-soglasovaniya-pereplanirovki-kvartiry.ru .

  8. LarryCoell表示:

    deneme bonusu veren siteler deneme bonusu veren siteler denemebonusu2026.com deneme bonusu veren siteler yerliarama.org

  9. ClydePab表示:

    ultrabet bonus: ultrabet yeni giris 1125 – ultrabet giris

  10. Hello, i think that i saw you visited my web site so i came
    to “return the favor”.I’m attempting to find things to enhance my
    website!I suppose its ok to use some of your ideas!!

  11. Medication information. What side effects can this medication cause?
    get zoloft without a prescription
    Everything news about drugs. Read now.

  12. What’s Taking place i’m new to this, I stumbled upon this I have
    found It absolutely helpful and it has aided me
    out loads. I am hoping to give a contribution & help different users like
    its helped me. Great job.

  13. CarrollUnton表示:

    deneme bonusu veren siteler yerliarama.org https://casinositeleri.win/# guvenilir casino siteleri

  14. rapi66表示:

    hello!,I like your writing so much! percentage we keep in touch more approximately
    your article on AOL? I require a specialist on this house to
    resolve my problem. May be that is you! Having a look forward to peer you.

  15. LarryCoell表示:

    Deneme Bonusu Veren Siteler Deneme Bonusu Veren Siteler Casino Siteleri

  16. Derekfar表示:

    Casino Siteleri: Casino Siteleri – guvenilir casino siteleri

  17. Income表示:

    Aw, this was an incredibly nice post. Spending some
    time and actual effort to generate a good article… but what
    can I say… I put things off a whole lot and
    never manage to get nearly anything done.

  18. CarrollUnton表示:

    deneme bonusu veren siteler mycbet.com http://denemebonusuverensiteler.top/# deneme bonusu veren siteler 2024

  19. CarrollUnton表示:

    deneme bonusu veren siteler https://slot-tr.online/# slot oyunlar?

  20. Тут можно преобрести сейф под оружие оружейный шкаф сейф купить

發佈留言

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