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
neurontin 100mg cost: neurontin capsule 600mg – canada neurontin 100mg lowest price
This Week’s Best Stories Concerning Shopping Online Sites Roof Bar System For Commercial Vans (Carole)
buy zithromax online australia: zithromax drug – zithromax for sale cheap
11 Ways To Completely Sabotage Your Which Is Best For Online Grocery Shopping Vimeo
amoxicillin for sale: amoxicillin 500 mg without prescription – amoxicillin 500mg capsule
I have been browsing on-line more than 3 hours today,
yet I by no means found any attention-grabbing article like
yours. It’s pretty price enough for me. Personally, if all web owners and
bloggers made excellent content material as you did, the net
will likely be much more helpful than ever before.
Double Glazing Repairs Near Me Tools To Make Your Daily Lifethe One Double Glazing Repairs Near Me Trick Every Individual Should Learn Double glazing Repairs near me
10 Times You’ll Have To Learn About Bonus Slots slot Volatility
5 Killer Quora Answers To SEO Ranking Software Seo Ranking Tool – https://Maps.Google.Kz/,
doxycycline 100mg: doxycycline 100mg capsules – doxycycline medication
Hi there to all, the contents present at this
site are really amazing for people knowledge, well, keep up the nice work fellows.
doxycycline 150 mg: doxylin – doxycycline 100mg
The 10 Most Scariest Things About Double Glazing Repairs Near
Me double glazing repairs
price of doxycycline: doxycycline – cheap doxycycline online
lisinopril tabs 40mg
The Secret Life Of Erb’s Palsy Lawyers erb’s palsy lawsuits
Avon Glimmerstick Diamonds Eyeliner: A Simple Definition avon glimmersticks eyeliner colors (Kathleen)
20 Resources That Will Make You More Effective At Buy Used Mobility Scooter buy Travel mobility scooter
Let’s be clear – just because you’ve had sex before,ロボット セックス it doesn’t mean you’ve signed up to it at any and every opportunity in the future.
20 Resources To Make You More Effective At Shopping Online Uk Clothes cheap online clothing stores with free shipping worldwide
Simply want to say your article is as astonishing. The clarity in your submit is simply nice and
that i could assume you’re a professional in this subject. Fine together with
your permission let me to take hold of your feed to stay updated with coming near near post.
Thank you one million and please keep up the enjoyable work.
amoxil generic: buy cheap amoxicillin online – amoxicillin 50 mg tablets
buy amoxicillin 250mg: cheap amoxicillin 500mg – purchase amoxicillin online without prescription
Hi it’s me, I am also visiting this website daily, this site is in fact good and the viewers are really sharing nice thoughts.
7 Tips About Upvc Window Repairs That Nobody Will Tell You upvc windows Repair
What’s up to every one, it’s really a pleasant for me to go to see this web
page, it consists of helpful Information.
The History Of Treatments Of Adhd treat adhd in adults
prednisone tablets: prednisone cost in india – prednisone cost canada
Five Killer Quora Answers On Shopping Online Uk Clothes shopping online uk
clothes – Gena,
See What Washing Machine Sale 12kg Tricks The Celebs Are Utilizing Washing Machine Sale 12Kg