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 Things You Learned In Kindergarden That’ll Help You With Repairs To Upvc Windows
upvc window repair (Stewart)
The 10 Most Terrifying Things About Auto Accident Attorneys auto Accident Attorneys
What Experts On Cerebral Palsy Lawsuit Want You To Know?
cerebral palsy lawyers
20 Fun Details About Online Shopping Uk Discount
Shopping online
What Will Slot Be Like In 100 Years? kaymell.uk
15 Shocking Facts About Railroad Injuries Lawyer That
You Didn’t Know About railroad injuries law Firms
Angels Bail Bonds Costa Mesa
769 Baker St,
Coosta Mesa, ⅭA 92626, United Statеs
what is bail bond business
Five Killer Quora Answers On Medication For Autism And ADHD medication for autism And adhd
I was recommended this website via my cousin. I am not positive whether this
submit is written by means of him as no one else recognize such specific about my trouble.
You are wonderful! Thank you!
Guide To Uk Online Shopping Sites Like Amazon: The Intermediate Guide The
Steps To Uk Online Shopping Sites Like Amazon online Shopping sites
See What Online Charity Shop Uk Clothes Tricks The Celebs Are Utilizing online charity Shop Uk clothes
10 No-Fuss Methods To Figuring Out Your Boat Accident Attorneys boat accident law firm
The 10 Scariest Things About Affordable Local SEO Services Near Me local seo services near me (Edythe)
What’s The Job Market For Double Glazed Window Repairs Professionals?
Double Glazed Window Repairs (https://Tujuan.Grogol.Us/Go/AHR0CHM6Ly90ZW1WYXN0ZS5Jb20VYUpkSWI2Ym9VWHU)
Do Not Buy Into These “Trends” Concerning Upvc Window Repairs window repairs near me
Five Killer Quora Answers To Dangerous Drugs Law Firm dangerous drugs law firm
Howdy! Someone in my Myspace group shared this site with us so I came
to take a look. I’m definitely enjoying the information. I’m book-marking and will be
tweeting this to my followers! Superb blog and outstanding
design.
The Three Greatest Moments In Asbestos And Mesothelioma History https://www.cassylawn.top/j9rg9-n8sb-3d5swdy-0x3r3-tt67-2818/
10 Quick Tips About Slot Strategies slot strategy
where can i buy cheap aldactone no prescription
There are four primary forms of cash advances — bank card cash
advances, payday loans, installment loans, and merchant cash advances.
Does Technology Make Auto Accident Law Better Or Worse?
auto accident lawsuit
10 Quick Tips About Dangerous Drugs Lawyer Dangerous drugs attorney
Why You Should Concentrate On Improving Slot Updates Low Variance slots
Dr. Aamir Safdar is Board Certified in Adult Psychiatry as well as Child and Adolescent
Psychiatry.
farxiga generic
Мы заботимся о том, чтобы вы испытали идеальное вращение с нашим бездепозитным бонусом на
бесплатное вращение и приветственными бонусными кодами R7.
How Private Psychiatrist North London Has Transformed My Life The Better london Private Psychiatrist
Lorem Ipsum has been the industry’s standard dummy text https://selfpas.it/qjgmx of type and scrambled it to make a type specimen book.
can i purchase advair diskus without a prescription