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
15 Gifts For The CSGO Most Profitable Cases Lover In Your Life wildfire case
Kudos, I enjoy it.
My brother suggested I would possibly like this web site.
He was once totally right. This submit truly made my day.
You can not believe just how a lot time I had spent for this
information! Thank you!
What’s The Job Market For Window Doctor Near Me Professionals?
Window Doctor near me
Ten Real Money Slots That Will Help You Live Better Slot symbols
16 Must-Follow Pages On Facebook For Cheap Online
Shopping Sites Uk Marketers Best headache Rack
The Little-Known Benefits Of Link Building Software
Best Backlink Builder Software
5 People You Oughta Know In The Door Repairs London Industry near Me
Pills information. Brand names.
where can i get cheap aricept without dr prescription
Some about medication. Read now.
What’s The Current Job Market For Best Rated Folding Treadmill Professionals?
best rated folding treadmill
9 . What Your Parents Teach You About France
Online Shopping Sites Clothes france online shopping sites clothes
The Ultimate Cheat Sheet For What CSGO Cases Should I Open Shadow case
Hi there! I could have sworn I’ve been to this website before but after going through many of the posts I
realized it’s new to me. Anyhow, I’m definitely delighted I found it
and I’ll be book-marking it and checking back often!
A Brief History Of Mobile Slots History Of Mobile
Slots Top software providers for slots; http://www.fjclwz.us,
9 Things Your Parents Teach You About Fela Accident Attorney fela Accident attorney
The 10 Scariest Things About Best Slots best slots (Launa)
pharmacies
9 Lessons Your Parents Teach You About Slot Strategies Slot Strategies
14 Misconceptions Commonly Held About Motorcycle Accident
Legal Motorcycle Accident Lawyers
10 Things You Learned In Preschool That Will Help You
With Car Accident Attorney Car Accident lawsuit
You really make it appear really easy together with your presentation however I find this matter to be really something which I feel I’d by no means understand. It kind of feels too complicated and very vast for me. I’m having a look forward for your subsequent submit, I will try to get the hang of it!
I blog frequently and I really thank you for your content.
This article has truly peaked my interest. I am going to bookmark your blog and keep checking for new details about once per week.
I opted in for your RSS feed too.
How To Resolve Issues With Slot Variance Low variance Slots
The 10 Most Terrifying Things About Medium Variance Slots Medium variance slots
Why Mercedes Key Fob Still Matters In 2023 genuine Mercedes key fob
Действительно….такой полезный онлайн сайт. Посетите также мою страничку https://www.printwhatyoulike.com/get_page?topic=59750.100&url=https://migration-bt4.co.uk/profile.php?id=400078
The 10 Most Scariest Things About Classic Casino Slots classic casino slots
how can i get cabgolin without prescription
8 Tips To Improve Your Ferrari Car Key Replacement Game ferrari Locksmith near Me
I’m curious to find out what blog platform you happen to be utilizing?
I’m experiencing some minor security issues with my latest
website and I’d like to find something more risk-free.
Do you have any suggestions?