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
Paragraph writing is also a fun, if you be acquainted with then you can write if not it is
difficult to write.
Five Killer Quora Answers To Window Repair Near Me window repair near me (http://gnsr.co.kr/bbs/board.php?bo_table=womanno3&wr_id=155467)
Hi, I do believe this is an excellent website. I stumbledupon it 😉 I am going to come back once again since i have bookmarked it.
Money and freedom is the greatest way to change, may you
be rich and continue to help others.
Я смог отыграть кэшбэк и выиграть денег только в этом казино.
See What How To Get An ADHD Diagnosis UK Tricks The Celebs Are Using get An adhd diagnosis uk
Hello there! get finasteride for sale good internet site.
An Asbestos Litigation lawyer can assist victims to file a lawsuit against employers
for exposure to the toxic mineral. A lawsuit can help victims receive compensation for
medical expenses, lost wages and past pain and suffering.
I love what you guys are up too. This type of
clever work and exposure! Keep up the fantastic works guys I’ve added you guys to
blogroll.
You Are Responsible For An Truck Accident Budget? 12 Best Ways
To Spend Your Money truck accident lawsuit
What’s The Current Job Market For Dangerous Drugs Law Firm
Professionals? dangerous Drugs law firm
I go to see day-to-day a few blogs and blogs to read content, except this web site provides quality based articles.
The Motor Vehicle Litigation Success Story You’ll Never Imagine motor vehicle accident law firm
The 10 Most Terrifying Things About 18 Wheeler Accident Lawyers 18 wheeler
Your style is unique in comparison to other folks I have read
stuff from. Many thanks for posting when you’ve got
the opportunity, Guess I will just bookmark this web site.
15 Documentaries That Are Best About Mesothelioma Compensation Mesothelioma claims
Also visit my web page: top 10 poker websites
Alpha Drive is a popular dietary supplement that is known for its ability to boost energy levels, enhance focus, and improve overall performance.
Feel free to explore web-site; Alpha Drive Ingredients
where can i buy cozaar
Meds information sheet. What side effects?
can i purchase azelastine without dr prescription
Best about medication. Get now.
I like the valuable info you provide in your articles. I’ll bookmark
your blog and check again here regularly. I’m quite sure I will learn many new stuff right here!
Best of luck for the next!
What’s The Most Important “Myths” About High Limit Slots Could Actually Be Accurate slot offers
With anybody of those Rene Rife wholesale lingerie units your accomplice is sure to fall flat on his again. Try of
lingerie, costumes and unmentionables that could have your partner craving
for more. Here at Clovia, you will be selecting the perfect
one according to your choice. There may be rather a lot more totally different from regular boring
whites to better of time ill-fitting and riding up the unsuitable
locations. However a plunging necklines will hinder your from carrying common bra.
My favorite is the bustiers, they’ve a slimming impact and will present the tuck and elevate important
to boosting your self-esteem. Accessibility: Whereas privacy is necessary, it’s additionally
important to store your lingerie in a means that allows
quick access when it’s essential retrieve them. Chances are you’ll not even need to speak together with your husband
at the moment but you will definitely really feel
a bond between you two.
20 Things You Should ASK ABOUT CS GO Case Battle Before Purchasing It Case Opening
Excellent, what a webpage it is! This website
provides valuable information to us, keep it up.
Hello! I simply would like to offer you a big thumbs up for the excellent information you have here on this post.
I will be returning to your blog for more soon.
The 10 Most Terrifying Things About Slot Promotions slot promotions – http://dott-comm.com/,
17 Reasons To Not Ignore Truck Accident Attorneys lawsuits
The 10 Most Terrifying Things About Online Shopping Stores List online Shopping Stores list
My brother suggested I would possibly like this blog. He was totally right.
This post actually made my day. You can not believe just how much time I had spent for this info!
Thank you!