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...

847,945 Responses

  1. 9 . What Your Parents Taught You About Upvc Window Repairs
    Near Me Upvc window repairs near me

  2. Five Killer Quora Answers To Fridge Freezer For Sale fridge freezer for Sale

  3. Ronaldpsymn表示:

    http://finasteride.store/# buying propecia without prescription

  4. Five People You Must Know In The Small Double Mattresses Industry double mattress sale uk

  5. Davidtes表示:

    cipro ciprofloxacin where can i buy cipro online buy cipro online without prescription

  6. RandallTaulp表示:

    lisinopril 4 mg: lisinopril 25 mg cost – buy lisinopril uk

  7. Eusebia表示:

    Ghost Installations Tips That Will Change Your Life
    ghost installations, Eusebia,

  8. Bunk Bed Online Store Tools To Ease Your Daily Lifethe One Bunk Bed Online
    Store Trick That Every Person Should Be Able To bunk Bed online

  9. Adolph表示:

    What The 10 Most Worst Bean Coffee Maker FAILURES Of All Time Could Have Been Prevented bean to cup machines (Adolph)

  10. Guide To Online Shopping Sites In United Kingdom: The Intermediate Guide In Online
    Shopping Sites In United Kingdom online shopping sites In united kingdom

  11. Davidtes表示:

    ciprofloxacin 500 mg tablet price cipro online no prescription in the usa buy cipro online

  12. quietmona.Com表示:

    14 Businesses Doing An Amazing Job At CSGO Cases Highest
    Roi cs20 case – quietmona.Com,

  13. See What Retro Espresso Machine Tricks The Celebs Are Making Use Of retro espresso Machine

  14. Hey there I am so delighted I found your website, I really found
    you by mistake, while I was looking on Bing
    for something else, Nonetheless I am here now and would just
    like to say thanks for a fantastic post and a all round enjoyable blog (I also
    love the theme/design), I don’t have time to browse it all at
    the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to
    read more, Please do keep up the fantastic work.

  15. Interesting blog! Is your theme custom made or did you download it
    from somewhere? A theme like yours with a few simple tweeks
    would really make my blog stand out. Please let me know where you got your theme.
    Bless you

    Here is my web page; 실시간중계

  16. Near By表示:

    Why You Should Concentrate On Making Improvements To Repairing Upvc Windows Near By

  17. titration表示:

    Titration ADHD Meds Tools To Ease Your Everyday Lifethe Only Titration ADHD Meds Trick
    That Should Be Used By Everyone Know titration

  18. Five Things You Didn’t Know About Ford Replacement Key Programming replacement keys for Ford (http://lineyka.org/)

  19. RandallTaulp表示:

    lisinopril brand name australia: buy lisinopril 20 mg without prescription – lisinopril 10 mg online no prescription

  20. 3 Common Causes For Why Your Keys For Mercedes Isn’t Working (And Solutions
    To Resolve It) Mercedes Replacement Keys

  21. 5 Killer Quora Answers To Psychiatry Assessment Uk Psychiatry Assessment Uk

  22. RandallTaulp表示:

    antibiotics cipro: ciprofloxacin mail online – cipro 500mg best prices

  23. b.cari.com.my表示:

    15 Mental Health Counselor Near Me Benefits That Everyone
    Should Know mental health assessments near me (b.cari.com.my)

  24. The Most Inspirational Sources Of Mobile Auto Diagnostic Services
    Near Me Engine Diagnostics

發佈留言

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