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,163,779 Responses

  1. Five Killer Quora Answers On How To Buy Clothes Online From Uk
    how to buy clothes online from uk

  2. A synthetic USP human growth hormone (somatropin).

    Look into my homepage: what does enable need decay mean

  3. Kathi表示:

    7 Simple Tips To Totally Refreshing Your Double Glazed Window Replacement Near Me double glazed windows replacement [Kathi]

  4. NewULife Ranks. Welcome to my NewUlife Testimonial.

    Here is my site: types of benefits disability

  5. Bookmarks表示:

    XYGENYX, a licensing business for FDA-registered products.

    Feel free to surf to my page :: Bookmarks

  6. 5 Killer Quora Answers On Best Lightweight Single Stroller lightweight Single stroller

  7. Allow me show you in my testimonial of NewULife.

    Visit my web page … ai headshot generator canva

  8. See What Online Shopping Stores In London Tricks The Celebs Are Making
    Use Of online shopping stores in london

  9. Medicine information sheet. Brand names.
    can i purchase arimidex without insurance
    Best about medicines. Read information here.

  10. I was curious if you ever thought of changing the page layout
    of your blog? Its very well written; I love
    what youve got to say. But maybe you could a little more in the way of
    content so people could connect with it better.

    Youve got an awful lot of text for only having 1 or two images.
    Maybe you could space it out better?

  11. In this instance, the life insurance policy firm’s money.

    my blog post – kinds of disability benefits

  12. Bookmarks表示:

    A synthetic USP human development hormone (somatropin).

    Also visit my site Bookmarks

  13. This vaporizer has a number of special features.

    Also visit my web-site; where to buy plant based meals

  14. Meds information. Brand names.
    can i buy generic celebrex for sale
    Best what you want to know about pills. Read now.

  15. Nice post. I was checking continuously this weblog and I am
    impressed! Extremely helpful info specially the closing section 🙂 I
    handle such information a lot. I was looking for this particular information for a long
    time. Thanks and best of luck.

    My blog post – 인천출장마사지

  16. The People Who Are Closest To Truck Accident Claim Share Some
    Big Secrets truck Accidents

  17. In recent years, CBD has gained significant popularity as a natural remedy for
    various physical and mental health issues.

    my internet site; Herbal Harmony CBD ingredients

  18. XYGENYX, a licensing business motion graphics for church free FDA-registered products.

  19. In recent years, the Keto Lyte Keto ACV Gummies diet has gained immense popularity as an effective way to shed those extra pounds and improve
    overall health.

  20. In this situation, the life insurance business’s cash.

    Feel free to surf to my homepage – what is data decay

  21. You’ll Be Unable To Guess Online Shop Designer Suits’s Secrets Online Shop Designer Suits, Tamikiti67.Hatenablog.Com,

  22. I’m the owner operator of GOPARTYBUS Party Bus Hire Prices Perth.

  23. Bookmarks表示:

    I most likely modified the whole publication 10 different times.

    Visit my site – Bookmarks

  24. Why You’re Failing At Beige Sectional Sofa
    http://www.4452346.xyz

  25. What is New U Life SOMADERM Transdermal
    Gel.

    My blog post – ai profile picture generator free reddit

發佈留言

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