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
online casino
The 12 Worst Types Of Users You Follow On Twitter online slots
lisinopril cancer recall
7 Things You Didn’t Know About Slot Tournaments casino slots
15 Weird Hobbies That Will Make You More Effective At SEO Consulting In London Search Engine Marketing london
9 Things Your Parents Taught You About Window Doctor Near Me window doctor near me
The 3 Greatest Moments In Modular Sectional Sofa History http://www.4452346.xyz
buy cheap albuterol
You suggested that really well.
What i do not realize is actually how you’re now
not actually much more neatly-appreciated than you might be
right now. You are very intelligent. You recognize therefore considerably
on the subject of this matter, produced me personally
believe it from numerous varied angles. Its like men and women are
not fascinated except it’s one thing to do with Lady gaga!
Your personal stuffs excellent. Always maintain it up!
The Top Reasons Why People Succeed In The Cerebral
Palsy Law Industry Cerebral Palsy Attorney
There’s certainly a great deal to learn about this topic.
I really like all of the points you have
made.
15 Motor Vehicle Lawyers Benefits Everyone Should
Be Able To motor Vehicle Accident attorney
Great post, you have pointed out some superb details, I
as wrll think thiss is a very good website.
Stop by my blog :: click
how to buy generic ashwagandha pills
The Hidden Secrets Of Motor Vehicle Settlement Motor Vehicle Accidents
The 10 Scariest Things About Mental Health Assessment Online
overall mental health Assessment
This Is The Professional Slots Case Study You’ll Never Forget
top winning slots
I know this website offers quality dependent articles and other stuff, is there any other web page which gives such
stuff in quality?
An experienced mind head injury lawyer los angeles legal representative
might be able to aid.
See What Online Charity Shop Uk Clothes Tricks The Celebs Are Using online charity shop uk Clothes
I am extremely impressed with your writing skills as well as with the
layout on your blog. Is this a paid theme or did you modify it yourself?
Anyway keep up the nice quality writing, it’s rare to see a great blog like this one nowadays.
can i get generic furosemide tablets
A Peek Inside Semi Truck Lawyer’s Secrets Of Semi Truck Lawyer semi truck accident Lawyers
The 10 Most Scariest Things About Motor Vehicle Law motor vehicle accident attorney (Collin)
Thibaut Nicolas Marc Courtois https://thhibaut-courtois.prostoprosport-fr.com Footballeur belge, gardien de but du club espagnol du Real Madrid . Lors de la saison 2010/11, il a ete reconnu comme le meilleur gardien de la Pro League belge, ainsi que comme joueur de l’annee pour Genk. Triple vainqueur du Trophee Ricardo Zamora
Greetings from Los angeles! I’m bored to tears at work so I decided to check out your blog on my iphone during lunch break.
I really like the info you provide here and can’t wait to take
a look when I get home. I’m shocked at how fast your blog loaded on my phone ..
I’m not even using WIFI, just 3G .. Anyways,
excellent site!
10 Misconceptions Your Boss Has About Semi Truck Claim Semi Truck Claim semi truck accident attorney
It’s a shame you don’t have a donate button! I’d certainly donate
to this fantastic blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account.
I look forward to brand new updates and will talk about this site with my Facebook group.
Chat soon! https://Bookmarkvids.com/story18463140/sigmaac
online casino