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,233,755 Responses

  1. TylerPhona表示:

    https://levitrav.store/# Buy Levitra 20mg online

  2. CharlesDog表示:

    http://kamagra.win/# Kamagra 100mg

  3. Hanna表示:

    10 Wrong Answers To Common Double Glazing Company Near
    Me Questions Do You Know The Correct Answers? double glazed door lock;
    Hanna,

  4. How Repairs To Upvc Windows Has Changed The History Of Repairs
    To Upvc Windows window repairs

  5. Repairman表示:

    Five Killer Quora Answers On Window Repairman Repairman

  6. Hello there, just became alert to your blog through Google, and found that it is really informative. I?m going to watch out for brussels. I?ll be grateful if you continue this in future. Numerous people will be benefited from your writing. Cheers!

  7. It’s amazing to pay a quick visit this website and reading the views of all mates about this paragraph,
    while I am also zealous of getting experience.

  8. Jeannine表示:

    Ten Taboos About Washer 10kg You Shouldn’t Share On Twitter washer 10kg (Jeannine)

  9. vimeo.Com表示:

    10 Injury Settlement-Related Injury Settlement-Related Projects That Will Stretch
    Your Creativity jackson injury lawsuit, vimeo.Com,

  10. ElmerWes表示:

    super kamagra kamagra oral jelly Kamagra tablets

  11. JamesNow表示:

    Buy Cialis online: buy cialis overseas – Cialis without a doctor prescription

  12. CasinoYYY表示:

    Join the excitement at YYY Casino and enjoy a world-class gaming experience from the comfort of your own home. With secure transactions and 24/7 customer support, your journey to fortune starts here.

  13. erb's Palsy表示:

    5 Killer Quora Answers On Erb’s Palsy Law erb’s Palsy

  14. The Most Underrated Companies To In The How To Buy Clothes
    Online From Uk Industry Brown Area Rug 10X15

  15. 10 Top Facebook Pages Of All Time Concerning Michael Kors Black Handbag michael kors bag grey

  16. 10 Things Your Competitors Can Teach You About Door Fitting Leeds
    composite door Repairs leeds

  17. How Do I Explain Upvc Window Repairs To A 5-Year-Old upvc Window Repairs near me

  18. The Evolution Of Tommy Hilfiger Women’s Bag Sale Tommy Hilfiger Bags
    (http://Fpcom.Co.Kr)

  19. You’ll Never Be Able To Figure Out This Window Repair Near Me’s Secrets window repair Near me

  20. 8 Tips To Enhance Your Seat Key Fob Replacement Game Seat Ibiza Key Fob Not Working

  21. I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get got an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly very often inside case you shield this hike.

  22. TylerPhona表示:

    http://cenforce.pro/# Purchase Cenforce Online

  23. Guide To Mesothelioma Compensation Fund: The Intermediate
    Guide In Mesothelioma Compensation Fund mesothelioma compensation Fund

  24. GeorgeSep表示:

    Levitra tablet price: Levitra generic price – п»їLevitra price

  25. JamesNow表示:

    over the counter sildenafil: Buy generic 100mg Viagra online – Generic Viagra for sale

  26. 10 Myths Your Boss Is Spreading About Non Stimulant Adhd Medication Non Stimulant Adhd
    Medication Adhd Adults Medication

  27. The 9 Things Your Parents Taught You About Double Glazed
    Window Suppliers Near Me double glazed Window suppliers near me

  28. 20 Things You Should Ask About Car Key Replacement Service Near Me
    Before You Purchase Car Key Replacement Service Near Me Remote Car Key Replacement

  29. Data Sdy表示:

    I believe everything posted was very logical. But, what about this?
    what if you added a little content? I ain’t saying your information is not good, however what if you added a post title that
    grabbed folk’s attention? I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室 is kinda vanilla.
    You should peek at Yahoo’s front page and watch how they create post titles to
    get people interested. You might add a related video or a
    picture or two to get people interested about everything’ve got to say.

    Just my opinion, it would make your posts a little
    bit more interesting.

  30. vimeo.com表示:

    Birth Injury Legal: What No One Has Discussed atoka birth injury lawsuit – vimeo.com

發佈留言

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