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
Kaka https://kaka.prostoprosport-br.com Futebolista brasileiro, meio-campista. O apelido “Kaka” e um diminutivo de Ricardo. Formado em Sao Paulo. De 2002 a 2016, integrou a Selecao Brasileira, pela qual disputou 92 partidas e marcou 29 gols. Campeao mundial 2002.
Medicines information leaflet. Short-Term Effects.
where to buy cheap arimidex without insurance
All about pills. Read information now.
order cheap imdur prices
where can i get generic methocarbamol without a prescription
If you wish for to take a great deal from this post then you have to apply such techniques to your won weblog.
Drugs prescribing information. Cautions.
buying generic lisinopril without rx
Best what you want to know about pills. Get now.
The Most Underrated Companies To Monitor In The Greenpower Mobility Industry Green Power Scooter
My partner and I stumbled over here by a different web
address and thought I may as well check things out. I like what I see so
now i am following you. Look forward to looking
into your web page again.
The 10 Scariest Things About Truck Accident Law Firm truck Accident law Firm
how to get generic remeron without insurance
Pills information leaflet. Short-Term Effects.
get cheap esomeprazole without rx
Everything about medicine. Read now.
The 10 Scariest Things About Best Slots Best Slots (Merigotoya.Hatenablog.Com)
This Story Behind Window Repair Near Will Haunt You For
The Rest Of Your Life! upvc Window repair Near me
Hello I am so excited I found your blog, I really found you by accident,
while I was researching on Yahoo for something else, Nonetheless I
am here now and would just like to say cheers for a fantastic post and a all round exciting blog (I also love the
theme/design), I don’t have time to browse it all at the minute but I have bookmarked it
and also added in your RSS feeds, so when I have time I will
be back to read much more, Please do keep up the awesome jo.
Asbestos cases can be complicated because they require multiple
jurisdictions. Law firms with experience in processing Asbestos case claims at the national and state levels know the specific
laws that apply.
Guide To Workers Compensation Attorney: The Intermediate Guide The Steps To Workers Compensation Attorney workers compensation
See What Cerebral Palsy Lawyer Tricks The Celebs Are Using
Cerebral Palsy Lawyer
http://google.la/url?q=https://ed-apteka.ru
The 10 Most Scariest Things About Private ADHD Assessment Adult private adhd assessment adult
What The 10 Most Worst High Wycombe Door Panels Errors Of All Time Could Have Been Prevented glazier High wycombe
Medication information sheet. Brand names.
buy generic synthroid pill
All what you want to know about medicine. Get now.
Medicament information for patients. Cautions.
get cheap atarax price
Actual trends of medicine. Read now.
buy depo medrol tablets
Pills information for patients. Effects of Drug Abuse.
can i order generic norvasc without prescription
All information about drug. Get information here.
Guide To 18 Wheeler Accident Attorney: The
Intermediate Guide To 18 Wheeler Accident Attorney
attorney
The One SEO Link Building Services Trick Every Person Should
Know Seo services Pricing
Generally in advertising, you’ll want to pay to play.
My blog post cosri air fryeer [https://Www.reddit.com/r/selfpromo/comments/1dnxbgd/cosori_air_fryer_review/]
Howdy! This blog post could not be written much better! Looking at this post reminds me of my previous roommate!
He continually kept preaching about this. I’ll send this article to him.
Fairly certain he’ll have a great read. Thank you for sharing!
You’ll Never Guess This Window Replacement Near Me’s Tricks window replacement near
me (Roxanne)
whoah this blog is great i really like reading your articles.
Keep up the good work! You realize, many people are looking round for this information,
you can help them greatly.
Feel free to visit my blog; fitspresso complaints