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

910,720 Responses

  1. MarvinRoave表示:

    amoxicillin online purchase where can i buy amoxicillin over the counter amoxicillin 1000 mg capsule

  2. Eloy表示:

    Watch This: How Key Fob Repair Near Me Is Taking Over The World And What You Can Do About It car remote key repair shop near me – Eloy

  3. MarvinRoave表示:

    gabapentin online prescription price for neurontin neurontin 300 600 mg

  4. Richardorele表示:

    neurontin cap 300mg: canada neurontin 100mg lowest price – neurontin singapore

  5. Henryfet表示:

    order prednisone 10 mg tablet: prednisone pills cost – buy prednisone canada

  6. Billyved表示:

    http://amoxila.pro/# amoxicillin 500mg no prescription

  7. Richardorele表示:

    prednisone 50 mg tablet canada: prednisone 5 mg brand name – prednisone 20mg buy online

  8. MarvinRoave表示:

    buying amoxicillin in mexico amoxicillin 500 mg tablets amoxicillin 500mg capsule buy online

  9. You really make it seem so easy with your presentation but I find this matter to be actually something that I think I
    would never understand. It seems too complicated and very broad for me.
    I am looking forward for your next post, I will try to get the hang of it!

  10. Drugs information. Generic Name.
    celebrex
    Some news about medicine. Get here.

  11. kilowatt Case表示:

    The Reasons To Focus On The Improvement Of CSGO Cases New kilowatt Case

  12. 10 Things That Your Family Teach You About Bentley Continental
    Gt Key Fob bentley continental gt key fob

  13. Genuine HGH Perks (NewULife Hgh Gel Not Evaluated Yet).

    My blog post rhodium price chart kitco

  14. 10 Websites To Help You Learn To Be An Expert In Malpractice
    Attorneys malpractice law Firm

  15. 5 Tools Everyone Is In The Slot Machines
    Industry Should Be Utilizing legitimate Slots

  16. MarvinRoave表示:

    buy amoxicillin 500mg online amoxicillin 875 125 mg tab buy amoxicillin 500mg capsules uk

  17. Undeniably believe that which you stated. Your favorite reason seemed to be
    on the web the easiest thing to be aware of. I say to you, I definitely get annoyed while
    people consider worries that they plainly do
    not know about. You managed to hit the nail upon the top
    and also defined out the whole thing without having side-effects , people
    can take a signal. Will probably be back to get more.
    Thanks

  18. Guide To Robot Vacuum For Pet Hair: The Intermediate Guide
    For Robot Vacuum For Pet Hair robot vacuum for pet hair (https://qooh.me/crowdbrass85)

  19. Billyved表示:

    http://gabapentinneurontin.pro/# brand name neurontin price

  20. Richardorele表示:

    amoxil generic: buy amoxil – over the counter amoxicillin

  21. What Is Planet Spa Pillow Mist? What Are The Benefits And How To Utilize It avon planet spa radiant gold

  22. MarvinRoave表示:

    neurontin 100 neurontin 2018 buy cheap neurontin online

  23. 10 Myths Your Boss Is Spreading Regarding Private ADHD Assessment Cost
    how Much does a private adhd assessment cost

  24. notabug.Org表示:

    10 Facts About Locksmith Car That Insists On Putting You In The Best Mood Mobile car key locksmith near me (notabug.Org)

  25. MarvinRoave表示:

    prednisone 1 tablet prednisone 50mg cost purchase prednisone

  26. 10 Things People Hate About Railroad Injuries Attorneys Thibodaux Railroad Injuries Attorney

  27. Wow, amazing weblog structure! How lengthy have you
    ever been running a blog for? you made blogging
    look easy. The entire look of your website
    is wonderful, as smartly as the content material!

  28. See What Dreame Bot L30 Ultra Tricks The Celebs Are Using dreame bot l30 ultra

  29. Billyved表示:

    http://gabapentinneurontin.pro/# neurontin prices generic

發佈回覆給「avon planet spa radiant gold」的留言 取消回覆

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