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
The 10 Most Scariest Things About Locksmith For Car Near Me Locksmith For Car Near Me
Meds information sheet. Short-Term Effects.
where can i get generic oxcarbazepine no prescription
Actual about medicines. Read now.
griseofulvin online pharmacy: fincar pharmacy – viagra uk online pharmacy
Ten Symptoms Of Anxiety Disorders Depression Myths That Aren’t Always The Truth symptoms of teenage depression
20 Reasons Why Bmw Spare Key Cost Will Never Be Forgotten Bmw
Smart Key; https://Emplois.Fhpmco.Fr,
Hmm it seems like your site ate my first comment (it was extremely long) so
I guess I’ll just sum it up what I submitted
and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog blogger but I’m still new to
the whole thing. Do you have any suggestions for newbie blog writers?
I’d genuinely appreciate it.
buy online pharmacy: adipex online pharmacy diet pills – euro pharmacy cialis
It’s amazing in support of me to have a web site, which is valuable
for my experience. thanks admin
How do I copyright my site?. How do I use a pic from other sites, if I hapened 2b blogging on something relating to that pics?.
clindamycin target pharmacy: online pharmacies uk – guardian pharmacy loratadine
can i buy viagra in a pharmacy: target pharmacy clomid price – kamagra 365 pharmacy
I am really enjoying the theme/design of your site.
Do you ever run into any internet browser compatibility issues?
A handful of my blog visitors have complained about my site
not operating correctly in Explorer but looks great in Firefox.
Do you have any tips to help fix this problem?
Moreover, crypto is decentralised with servers all over the world — there are over ten thousand
nodes keeping track of all the transactions processed on the system.
pharmacy pattaya kamagra: clomid online pharmacy no prescription – frys pharmacy
Клининговая компания уборка квартиры после пожара http://uborka-ot-pozhara.ru/
What’s up all, here every person is sharing such familiarity, so it’s pleasant
to read this webpage, and I used to pay a visit this website daily.
best online pharmacy no prescription cialis: order xenical online pharmacy – anti fungal
My brother recommended I might like this website.
He used to be totally right. This post truly made my day.
You cann’t believe just how so much time I had spent for this information! Thank you!
Heya i am for the first time here. I found this board and I to find It truly useful & it helped me out much.
I hope to provide something back and help others like you helped
me.
It’s actually a nice and helpful piece of info. I am satisfied that you shared this helpful information with us.
Please keep us up to date like this. Thank you
for sharing.
https://vermilion-elephant-dd3cm3.mystrikingly.com/blog/ce2c120f5a0
Howdy are using WordPress for your site platform?
I’m new to the blog world but I’m trying to get started and
create my own. Do you require any html coding expertise
to make your own blog? Any help would be greatly appreciated!
5 Killer Quora Answers To Private ADHD Assessment London Private adhd assessment London
What Is The Reason Adding A Key Word To Your Life
Will Make All The Impact 3 wheel scooters for sale
near me (http://legendawiw.ru)
I’m not that much of a internet reader to be honest but your sites
really nice, keep it up! I’ll go ahead and bookmark your site to come back down the road.
Many thanks
Pretty section of content. I just stumbled upon your website and in accession capital
to assert that I acquire in fact enjoyed account your blog posts.
Anyway I’ll be subscribing to your augment and even I achievement you access
consistently quickly.
Three Reasons To Identify Why Your Robot Vacuum Isn’t Working (And
What You Can Do To Fix It) Best mop and vacuum robot combo
viagra us pharmacy online: mirtazapine pharmacy – ambien cr online pharmacy
rx discount pharmacy dade city fl: spain pharmacy viagra – mexican pharmacy adipex
rx care pharmacy pearland tx: clobetasol propionate online pharmacy – lamotrigine pharmacy prices