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
Why You Should Concentrate On Improving Erb’s Palsy Attorney
erb’s palsy lawyer
Responsible For A Cerebral Palsy Claim Budget? 12 Top Ways To
Spend Your Money cerebral palsy law firms
Comprehensive Guide to Mac Antivirus Software: Protection, Features, and Recommendations
The People Who Are Closest To Anxiety Treatment Tell You Some Big Secrets anxiety treatment Herbal
Your Family Will Be Grateful For Having This Play Casino Slots best Payouts slots
1win казино
I used to be recommended this web site via my cousin. I am not positive whether or not
this post is written by him as nobody else recognise such
targeted approximately my problem. You’re incredible!
Thank you!
Compensation can also be simple with brief time period loans.
How To Become A Prosperous SEO London Even If You’re Not Business-Savvy Affordable Seo London
Dangerous Drugs Tools To Ease Your Daily Life dangerous drugs Lawsuits
Getting Tired Of Semi Truck Lawsuits? 10 Inspirational Ideas To Revive Your Passion Semi Truck Accident Lawsuit
What To Focus On When Improving Personal Injury Attorney personal injury law firm
7 Things You’ve Never Learned About Mesothelioma
Lawyer Mesothelioma attorney
5 Killer Quora Answers To Slot Gaming Experience slot gaming experience
bookmarked!!, I love your site!
10 Facts About Advanced Mobility Scooters That Insists
On Putting You In Good Mood compact Lightweight scooters
Medicament prescribing information. Cautions.
can i purchase generic effexor online
All what you want to know about pills. Read information here.
The 10 Scariest Things About Waitrose Groceries Online Shopping Uk waitrose Groceries online shopping Uk
I used to be able to find good information from your blog posts.
The 10 Most Scariest Things About 1kg Coffee Beans Uk 1kg Coffee beans uk [cart.cbic.co.jp]
20 Reasons Why Car Accident Lawyers Will Not Be Forgotten car accidents
May I simply say what a comfort to discover a person that really understands
what they are discussing over the internet. You actually realize
how to bring an issue to light and make it important.
A lot more people have to check this out and understand this side of your story.
I was surprised you aren’t more popular because you
most certainly possess the gift.
Appreciate the recommendation. Let me try it out.
The 9 Things Your Parents Taught You About Single Stroller
For Sale single stroller for sale (https://www.birkbyjuniorschool.co.uk/)
10 Things That Your Family Teach You About Slot Strategies Slot Strategies
How To Create An Awesome Instagram Video About Replacement Fiat 500 Key spare key Fiat 500
The History Of Kids Bunk Bed In 10 Milestones bunk beds usa (M.w-ww.blackpeach.co.kr)
can i buy actos without a prescription
Hello, I would like to subscribe for this weblog to obtain newest updates,
so where can i do it please help.
Your cash is locked away till you repay the mortgage.