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

988,084 Responses

  1. Themed Slots表示:

    You’ll Never Guess This Licensed Slots’s Benefits Themed Slots

  2. Slot Strategy表示:

    20 Irrefutable Myths About Slot Symbols: Busted Slot Strategy

  3. Birth Defects表示:

    10 Things Everybody Gets Wrong About The Word “Birth Defect Lawsuit” Birth Defects

  4. The Worst Advice We’ve Seen About Semi Truck Lawsuits Semi Truck Lawsuits semi truck accident Lawsuit

  5. MichaelBot表示:

    Overview to Imitation Premium Purses

    Reasons of an Appeal for Replica Upscale Luggage

    Version Designer Handbags Possess Risen In Popularity DueTo Numerous Compelling Causes

    Cost Represent Commonly A Prompting Factor

    Outstanding Replicas Present A Attraction About Label Labels WithNo The Substantial Expense
    Advances With Manufacturing Represent Which Those Copies Commonly Have Exceptional Equivalencies To A Originals

    With Them Which Value Style But Represent Budget-Conscious

    Replica Luxury Purses Supply A Inviting Replacement

    Advice With Spotting Validity

    Scrutinizing Elements Including Logos Stitching With Accessories

    Will Aid Decide An Replica’s Validity

    Comparing These Features With Pictures Of Real Designer Baggage

    Might Expose Several
    Alternatives InPlaceOf Imitation Premium Baggage

    AffordablyPriced Designer Equivalents

    Be Achievable That Give Fashionable Choices WithNo Compromising With High Standards

    Those Items Are Formally Built From Manufacturers Who Offer Premium Vogue With Accessible

  6. Cathy表示:

    15 Things You Don’t Know About Window Seal Replacement glass window replacement near me
    (Cathy)

  7. bokep barat表示:

    Good article. I absolutely love this website. Stick with
    it!

  8. Medicines prescribing information. Long-Term Effects.
    can i get phenytoin online
    Some what you want to know about meds. Read information here.

  9. 11 “Faux Pas” That Are Actually Acceptable To Make With Your Trucking
    Lawyers Truck Accident Law Firm

  10. 10 Simple Ways To Figure Out Your High Roller Slots Slot Providers, Articlement.Com,

  11. What’s The Current Job Market For Treadmill Foldable Electric Professionals Like?

    Treadmill Foldable Electric

  12. 5 Killer Quora Answers To Double Travel Stroller double travel stroller, flutelizard98.Bravejournal.net,

  13. 5. Erb’s Palsy Lawyers Projects For Any Budget erb’s Palsy lawsuit

  14. vimeo.com表示:

    15 Reasons To Not Ignore Uk Online Grocery Shopping Sites Air Control
    Damper For 6″ Duct (vimeo.com)

  15. A List Of Common Errors That People Do With Online Casino
    Slots online slot machines

  16. 10 Things Everybody Hates About Play Slots best payouts slots

  17. What’s The Job Market For Online Shopping Clothes Uk Cheap
    Professionals Like? Online shopping Clothes uk cheap

  18. fun Slots表示:

    17 Signs To Know If You Work With Slot Promotions fun Slots

  19. I truly love your website.. Very nice colors & theme. Did you build this website
    yourself? Please reply back as I’m attempting to create my very own website and would like to know where you got this from or
    exactly what the theme is called. Thank you!

發佈留言

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