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
Veleco Mobility Scooter For Sale Tools To Make Your Everyday Lifethe Only Veleco Mobility Scooter For
Sale Trick Every Individual Should Learn veleco mobility scooter for sale
25 Shocking Facts About Slot Bonuses Slots With Bonuses
See What Veleco Mobility Scooters Reviews Tricks The Celebs Are Using veleco mobility scooters reviews
Feel free to surf to my blog post: free sex partner near me
You’ll Never Be Able To Figure Out This Single Lightweight Stroller’s Secrets single lightweight stroller [http://www.mc-ala.Org]
This Is A SEO Agency In Uk Success Story You’ll Never Imagine seo Agency birmingham
Five Killer Quora Answers On Slot Sound Effects slot sound effects
A little point, yet one that perhaps shows the subtle social distinction in between the words.
Feel free to surf to my webpage https://nlifelab.org/bbs/board.php?bo_table=free&wr_id=2569044
Lorem Ipsum has been the industry’s standard dummy text https://stepsdown.cfd/giotmp of type and scrambled it to make a type specimen book.
What Experts In The Field Would Like You To Learn replace lock on upvc door
Hello my loved one! I want to say that this article is amazing, nice
written and come with almost all important infos.
I would like to see extra posts like this .
Think You’re Perfect For Doing Semi Truck Lawsuit?
Check This Quiz Semi truck accident lawyers
can i purchase cheap sinemet no prescription
10 Beautiful Images To Inspire You About Motor Vehicle Legal motor vehicle Accident lawyers
Hey would you mind letting me know which webhost you’re using?
I’ve loaded your blog in 3 different web browsers and I must say this blog loads a lot quicker then most.
Can you recommend a good internet hosting provider at a reasonable price?
Cheers, I appreciate it!
15 Documentaries That Are Best About ADHD Medication Titration adhd titration meaning (Denisha)
Are You Responsible For An Semi Truck Law Budget?
12 Top Notch Ways To Spend Your Money Semi truck accidents
How To Get Better Results Out Of Your Motor Vehicle Attorney motor vehicle Accident lawsuits
I couldn’t resist commenting. Very well written!
can you get celexa prices
No matter if some one searches for his essential thing,
so he/she wants to be available that in detail, thus that thing is maintained
over here.
where buy vasotec
https://maps.google.co.ls/url?q=https://dzheneriki-viagra.ru
What’s The Current Job Market For Cheap Single Stroller Professionals Like?
cheap single stroller
What’s up to all, the contents present at this site
are really awesome for people knowledge, well, keep up the nice work fellows.
Five Killer Quora Answers To Cheap Single Pushchair Cheap single pushchair
What Is The Reason Why Cheap Nespresso Machine Are So Helpful When COVID-19 Is In Session nespresso espresso machines (http://www.buehnehollenthon.at)
It is appropriate time to make some plans for the future
and it is time to be happy. I’ve read this post and if I could I
desire to suggest you some interesting things or suggestions.
Perhaps you could write next articles referring
to this article. I want to read even more things
about it!
You’ll Be Unable To Guess Window Repair Near Me’s Tricks window repair near me – list-lane-2.blogbright.net,
An asbestos lawyer can assist victims file a lawsuit against their employers for exposure to the toxic mineral.
A lawsuit could allow victims to recover reimbursement for medical bills, lost wages and past suffering and pain.