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
You’ll Be Unable To Guess Online Clothes Shopping Websites Uk’s Tricks online clothes Shopping Websites uk (validator.W3.org)
Five Killer Quora Answers To Federal Railroad Federal Railroad (Upriserz.Link)
11 Strategies To Refresh Your Erb’s Palsy Claim Erb’s Palsy Law Firm
Medicament information for patients. Generic Name.
get cheap azelastine pills
Everything trends of medicine. Get information now.
17 Signs You’re Working With Best Slots popular Slots
15 Search Optimisation Benefits That Everyone Should Know Marketing
20 Inspirational Quotes About Video Slots slot machine tips; https://easybookmark.win/story.php?title=17-signs-you-work-with-slot-machine-games,
It’s nearly impossible to find experienced people in this particular topic,
however, you seem like you know what you’re talking about!
Thanks
Hi there, I log on to your new stuff daily. Your story-telling style is awesome, keep doing what you’re doing!
A Productive Rant Concerning Dangerous Drugs Law Firm Dangerous Drugs Law Firms
In recent years, the ketogenic diet has gained popularity for its potential health benefits, including weight loss, improved energy levels, and better mental focus.
My site – http://dsmet.net/bbs/board.php?bo_table=free&wr_id=171252
how can i get prednisone price
The Reasons You Should Experience New Slots Online At The Very Least
Once In Your Lifetime reputable slots (peatix.com)
10 Facts About Wall Mount Fireplaces That Will Instantly Get You Into A Great Mood lynnbolvin.top
Hi to every one, the contents existing at this site are really awesome for people experience, well, keep up the nice work
fellows.
Asbestos Lawyer
is made up of a collection of minerals that are naturally
occurring. They are resistant to rust, durable and fire retardant.
They were previously widely used in the construction industry, but they have been removed due to health risks.
10 Double Size Mattress-Friendly Habits To Be Healthy double Mattress bed (rcin.Org.pl)
Drugs prescribing information. Short-Term Effects.
where to get atarax prices
Best trends of drug. Get information here.
Seasonal allergic rhinitis support
The No. One Question That Everyone Working In Online Shopping Sites Should Be Able To Answer
Online shopping sites for dress
What’s The Job Market For Uk Online Phone Shopping Sites Professionals?
Uk Online Phone Shopping Sites
can you get forxiga price
Woah! I’m really digging the template/theme of this website.
It’s simple, yet effective. A lot of times it’s very hard how to do local SEO get that “perfect balance” between user friendliness and visual appearance.
I must say you’ve done a amazing job with this. Additionally, the blog loads
very quick for me on Firefox. Excellent Blog! – Creating
A Consistent Local SEO-http://news.worldsharemarketlive.com/story/446185/boost-business-visibility-with-local-seo-dont-be-invisible-to-potential-customers-for-online-searches.html
Jude Bellingham https://jude-bellingham.prostoprosport-br.com Futebolista ingles, meio-campista do clube espanhol Real Madrid e do Selecao da Inglaterra. Em abril de 2024, ele ganhou o premio Breakthrough of the Year do Laureus World Sports Awards. Ele se tornou o primeiro jogador de futebol a recebe-lo.
9 Things Your Parents Teach You About Couches Sale Couches Sale
The 10 Scariest Things About Green Power Mobility Scooter Power mobility scooter
20 Important Questions To ASK ABOUT Cerebral Palsy Claim Prior To Purchasing Cerebral Palsy Claim cerebral
palsy law firm (doc.hypra.fr)
Medicines information. What side effects?
where can i buy zithromax without rx
Some what you want to know about medication. Get here.
How Much Can Small American Fridge Freezer Experts Make? Zack Foxworth