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,478,010 Responses

  1. Nikefut表示:

    darkmarket link deep web links darknet site

  2. Arthur Davis表示:

    Whether you are actually a brand new driver or skilled, Car insurance policy in Pasadena TX is crucial. You’ll locate many plans for Car insurance in Pasadena TX that offer really good market value for your amount of money car insurance company

  3. DarioTob表示:

    vavada kazi: vavada-kazi.ru – вавада казино онлайн

  4. HenrydeT表示:

    вавада казино онлайн vavada-kazi.ru казино вавада

  5. Medicament information sheet. What side effects?
    order cheap levonorgestrel without dr prescription
    Everything trends of medicine. Get now.

  6. выведение из запоя воронеж стационар bija089.0pk.me/viewtopic.php?id=2556 .

  7. Micheal Gray表示:

    I like just how you broke down the value of keyword research study in search engine optimization advertising. It’s crucial for driving targeted web traffic to your site. For those seeking extra suggestions, see seo firms

  8. Keluaran Hk表示:

    What’s Going down i’m new to this, I stumbled upon this I have discovered It absolutely useful and it
    has helped me out loads. I am hoping to contribute &
    aid different customers like its aided me. Great job.

    http://w1.bocorantogelhariini.cfd/

  9. I am sure this post has touched all the internet viewers, its really
    really fastidious paragraph on building up new webpage.

  10. Click Here表示:

    My spouse and I stumbled over here from a different web address and thought I might check things out.
    I like what I see so now i am following you. Look forward to exploring your
    web page again.

  11. HenrydeT表示:

    vavada vavada kazi vavada-kazi.ru

  12. CurtisIncix表示:

    пин ап кз: pin up казино – pin up казино

  13. DarioTob表示:

    пин ап кз: pinup-kazi.kz – пинап казино

  14. It’s great that you are getting ideas from this piece of writing as well
    as from our argument made at this time.

  15. Bland nya casinon som kommer att bli poppis under februari hittar vi Wishmaker, Jaak, Casiplay, Playzee som vi redan har hunnit ta en ordentlig titt på.

  16. Greetings I am so excited I found your website, I really found you by error, while I was browsing on Bing for
    something else, Anyhow I am here now and would just like to say many thanks for a marvelous post
    and a all round enjoyable blog (I also love the
    theme/design), I don’t have time to look over it all at the moment
    but I have saved it and also included your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the great
    work.

  17. DarioTob表示:

    pinup kazi: пин ап казино онлайн – pinup-kazi.kz

  18. HenrydeT表示:

    пинап казино пин ап казино pinup-kazi.ru

  19. Very energetic article, I loved that bit. Will there be a part 2?

    My blog post – what is pro nerve 6

  20. CurtisIncix表示:

    пинап казино: pinup – pinup kazi

發佈留言

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