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
A payout percentage is an quantity paid back to players more than Check out the post right here lengthy
term.
Hi, i feel that i saw you visited my blog so i came to go back the desire?.I’m attempting to
to find things to enhance my site!I guess its good enough to use
some of your concepts!!
Hi there, constantly i used to check blog posts here in the early hours in the
morning, since i like to find out more and more.
конференц зал оборудование оснащение конференц зал оборудование оснащение .
конференц оборудование oborudovanie-konferenc-zalov11.ru .
The GSA SEO Indexer is a powerful tool that can help your web pages place greater on these online
search engine.
My website; Gsa ser link list
оборудование для конференц залов оборудование для конференц залов .
You do NOT want to consume Lion’s Hair Mushrooms raw.
Here is my web blog: Bookmarks
Why We Love ADHD Diagnosis Private (And You Should, Too!) How Can I Get Diagnosed With Add
оборудование конференц зала оборудование конференц зала .
Window Repairs Near Me Tools To Make Your Daily Lifethe One Window Repairs Near Me Trick That Every Person Must Learn Window Repairs Near Me – https://Sword.Studio –
звуковое оборудование для конференц зала звуковое оборудование для конференц зала .
Some individuals vape flavorful nicotine
My blog post: Bookmarks
I’m not sure where you’re getting your info, but great topic.
I needs to spend some time learning more or understanding more.
Thanks for great info I was looking for this information for my mission.
Asbestos compensation victims seek compensation from companies responsible for asbestos exposure.
This is done through personal injury and wrongful death claims.
An experienced attorney can assist victims
and families through the process.
where to buy generic motilium without prescription
Hey there, You’ve done an excellent job. I’ll certainly digg
it and personally suggest to my friends. I’m confident they’ll be benefited from
this web site.
Howdy! finasteride no prescription great site.
NewULife Ranks. Welcome to my NewUlife Testimonial.
Review my web page … Motion blur image online free
BetMGM presents mobile wagering close to Nationals Park, and Caesars Sportsbook delivers
mobile wagering near Capital 1 Arena.
Also visit my web blog: Have a peek at this website
Drugs information sheet. Drug Class.
can you get proscar tablets
Best trends of drug. Read now.
Medication information. Short-Term Effects.
can you buy generic anastrozole tablets
Some news about medicament. Get information here.
NewULife Ranks. Invite to my NewUlife Testimonial.
My homepage :: Bookmarks
10 Things You Learned In Kindergarden That Will Help You With Windows
Birmingham Door Repairs birmingham
You’ll Never Guess This Shopping Online Uk’s Secrets shopping Online uk
USP human growth hormonal agent (somatropin).
my web blog: content of the report example
There is clearly a bundle to identify about this.
I assume you made some good points in features also.
Here is my web site: Bookmarks (Clifton)
amoxil 500 mg during pregnancy
Meds information leaflet. What side effects?
promethazine cough syrup for sale
Best information about medication. Get information now.
buy cheap imitrex pill