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,141,282 Responses

  1. 10 Things Everyone Has To Say About CSGO Most Profitable Cases
    CSGO Most Profitable Cases operation wildfire case (http://Www.dermandar.com)

  2. Medicament information for patients. Short-Term Effects.
    how to buy generic ipratropium without rx
    All information about drugs. Get information now.

  3. Drugs prescribing information. Generic Name.
    can you buy spiriva price
    Everything news about medication. Read information now.

  4. 5 Killer Quora Answers To Replacement Double Glazing
    Units Near Me Double glazing Units Near Me

  5. 9 . What Your Parents Teach You About Car Key Replacement Service
    Near Me Car Key Replacement Service Near Me (http://Www.Yonginmarathon.Com)

  6. I constantly spent my half an hour to read this blog’s content everyday
    along with a mug of coffee.

  7. live totobet表示:

    Livetotobet adalah situs news berita olahraga sepakbola terupdate dengan menyajikan fakta seputar dunia olahraga sepakbola semoga informasi yang diberikan oleh live totobet dapat
    bermanfaat untuk penggemar berita sepakbola dunia

  8. Do You Think You’re Suited For Double Glazing Company Near Me?
    Try This Quiz windows double glazing

  9. What’s The Most Common Mazda Key Debate Could Be As Black And White As You Might Think Mazda Cx 5 Keys Locked In Car

  10. pap kontol表示:

    I quite like reading through a post that will make people think.
    Also, thank you for permitting me to comment!

  11. This piece of writing will assist the internet
    people for setting up new webpage or even a weblog from start to end.

  12. Say “Yes” To These 5 Upvc Door Panels Tips Upvc Door Panels Replacements

  13. Marcel表示:

    What’s The Job Market For Double Glazed Repairs Near Me Professionals Like?
    double glazed repairs near me (Marcel)

  14. A Step-By Step Guide To Railroad Injuries Law railroad Injuries Law firm

  15. I discovered more a new challenge on this weight loss issue. A single issue is a good nutrition is tremendously vital while dieting. A tremendous reduction in bad foods, sugary food items, fried foods, sugary foods, red meat, and white flour products could be necessary. Retaining wastes parasites, and poisons may prevent goals for fat-loss. While particular drugs briefly solve the challenge, the awful side effects aren’t worth it, and in addition they never give more than a non permanent solution. This is a known proven fact that 95 of celebrity diets fail. Thanks for sharing your thinking on this web site.

  16. Ten Pinterest Accounts To Follow About Slot Updates low variance slots

  17. Overwhelmed by how to start the recovery of your QIWI wallet funds?

    Our customer support is on standby around the clock to guide you through the procedure and answer
    any questions you may encounter.

    With us, you’re opting for a reliable helper in getting your money back.

  18. Slot games表示:

    How To Determine If You’re Ready For Slot Developers Slot games

  19. 12 Companies That Are Leading The Way In Door Repair Near Me Window Replacement Near Me

  20. law表示:

    Unexpected Business Strategies For Business That Aided
    Truck Accident Attorney Near Me Succeed law

  21. What’s The Job Market For Popular Slots Professionals? Popular slots (muabanthuenha.com)

  22. unique Slots表示:

    The 9 Things Your Parents Taught You About Unique Slots unique Slots

  23. 5 Laws That Will Help Industry Leaders In Slot Games Industry
    top developer slots

  24. But strangers’ concerns and judgmental opinions reflect more on our outdated notions about pregnancy and the stigma against sex work than they do on Summer’s choice.Sebastian,ラブドール えろ

  25. You’ll Be Unable To Guess Top Slots’s Tricks top slots (gdchuanxin.com)

  26. Hi there! This is my first visit to your blog!
    We are a team of volunteers and starting a new initiative in a community in the same niche.

    Your blog provided us valuable information to work on. You have done a
    wonderful job!

  27. The 9 Things Your Parents Teach You About Best Online Clothing Sites Uk best Online clothing sites uk

  28. What Is Can I Buy From A Uk Website And Why Is Everyone Dissing It?
    Long-Lasting Humidifier Filters

  29. emergency表示:

    Five Things Everyone Makes Up On The Subject Of Window Repairman emergency

發佈留言

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