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
10 Sites To Help You Learn To Be An Expert In Birth Defect Legal birth Defect Attorneys
Guide To Double Glazing Window Repairs: The Intermediate Guide
In Double Glazing Window Repairs Window Repair
– https://Wren-Walsh-2.Hubstack.Net/,
What’s The Current Job Market For Online Shopping Uk Groceries Professionals Like?
Online Shopping Uk Groceries
20 Resources To Help You Become Better At Boat Accident Compensation boat accident law firms (luxuriousrentz.com)
can you buy allegra without prescription
Truck Accident Lawyer For Hire 10 Things I’d Like To Have Known Earlier Truck Accident Lawsuits
We’ve Had Enough! 15 Things About Erb’s Palsy Lawyer We’re Tired Of Hearing erb’s Palsy law Firms
Why You Should Focus On Enhancing Double Glazed Near Me cheapest
can i buy cheap motilium pills
10 Things That Your Family Teach You About Where To Buy Mobility Scooter where to buy mobility scooter
Are You In Search Of Inspiration? Look Up Sectional Sofas Near Me http://www.4452346.xyz
cost of cheap prednisone prices
15 Of The Most Popular 18-Wheeler Accident Lawyer Bloggers You Should Follow 18 Wheeler Accident lawsuit
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
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!
5 Killer Quora Answers To Shopping Online Uk Clothes shopping
online uk clothes (clients1.google.com.eg)
Ten Best 18 Wheeler Accident Lawyers That Really Improve Your Life 18 Wheeler Accident Lawyers
What’s The Job Market For Double Glazing Repairs Near Me Professionals?
double glazing repairs near me
14 Creative Ways To Spend Extra Truck Accident Compensation Claims Budget Truck Accidents
Guide To Malpractice Attorney: The Intermediate Guide In Malpractice Attorney malpractice attorney (Dolores)
9 Signs That You’re A Secondary Double Glazing Near Me Expert double galzed windows
(cse.google.co.za)
What Is The Reason L Sectional Sofa Is The Right Choice For You?
4452346.xyz
how to get tizanidine prices
Medicament information sheet. Generic Name.
mobic for sale
Actual information about drug. Read information now.
What’s up, its nice article concerning media
print, we all be familiar with media is a impressive source
of information.
The 10 Scariest Things About Hire Truck Accident Attorney Truck Accident Attorney
How To Save Money On Novice Slots demo slots (Anastasia)
These Are The Most Common Mistakes People Make When Using Modern Slots casino slot machines (weheardit.Stream)
buy albuterol over the counter
where can i get cheap prednisone pill