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,156,512 Responses

  1. 10 Sites To Help You Learn To Be An Expert In Birth Defect Legal birth Defect Attorneys

  2. Guide To Double Glazing Window Repairs: The Intermediate Guide
    In Double Glazing Window Repairs Window Repair
    https://Wren-Walsh-2.Hubstack.Net/,

  3. What’s The Current Job Market For Online Shopping Uk Groceries Professionals Like?

    Online Shopping Uk Groceries

  4. 20 Resources To Help You Become Better At Boat Accident Compensation boat accident law firms (luxuriousrentz.com)

  5. Truck Accident Lawyer For Hire 10 Things I’d Like To Have Known Earlier Truck Accident Lawsuits

  6. We’ve Had Enough! 15 Things About Erb’s Palsy Lawyer We’re Tired Of Hearing erb’s Palsy law Firms

  7. cheapest表示:

    Why You Should Focus On Enhancing Double Glazed Near Me cheapest

  8. 10 Things That Your Family Teach You About Where To Buy Mobility Scooter where to buy mobility scooter

  9. Are You In Search Of Inspiration? Look Up Sectional Sofas Near Me http://www.4452346.xyz

  10. 15 Of The Most Popular 18-Wheeler Accident Lawyer Bloggers You Should Follow 18 Wheeler Accident lawsuit

  11. Best Online Shopping Sites For Clothes Techniques To Simplify
    Your Everyday Lifethe Only Best Online Shopping Sites For Clothes Trick That Everybody Should Know best online Shopping sites for clothes

  12. boy789表示:

    Hi there! This blog post could not be written much
    better! Reading through this article reminds me of my previous roommate!
    He continually kept preaching about this.
    I will send this information to him. Fairly
    certain he’s going to have a good read. Many thanks for sharing!

  13. 5 Killer Quora Answers To Shopping Online Uk Clothes shopping
    online uk clothes (clients1.google.com.eg)

  14. Ten Best 18 Wheeler Accident Lawyers That Really Improve Your Life 18 Wheeler Accident Lawyers

  15. What’s The Job Market For Double Glazing Repairs Near Me Professionals?
    double glazing repairs near me

  16. 14 Creative Ways To Spend Extra Truck Accident Compensation Claims Budget Truck Accidents

  17. Dolores表示:

    Guide To Malpractice Attorney: The Intermediate Guide In Malpractice Attorney malpractice attorney (Dolores)

  18. 9 Signs That You’re A Secondary Double Glazing Near Me Expert double galzed windows
    (cse.google.co.za)

  19. 4452346.xyz表示:

    What Is The Reason L Sectional Sofa Is The Right Choice For You?
    4452346.xyz

  20. Medicament information sheet. Generic Name.
    mobic for sale
    Actual information about drug. Read information now.

  21. What’s up, its nice article concerning media
    print, we all be familiar with media is a impressive source
    of information.

  22. The 10 Scariest Things About Hire Truck Accident Attorney Truck Accident Attorney

  23. Anastasia表示:

    How To Save Money On Novice Slots demo slots (Anastasia)

  24. These Are The Most Common Mistakes People Make When Using Modern Slots casino slot machines (weheardit.Stream)

發佈留言

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