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
At this time it appears like Drupal is the best blogging platform out there right now.
(from what I’ve read) Is that what you’re using on your blog?
HorsePower Brands Omaha
2525 N 117tһ Ave #300,
Omaha, NE 68164, United States
14029253112
services industry home
Hey this is kinda of off topic but I was wanting to know if blogs use
WYSIWYG editors or if you have to manually code with HTML. I’m starting
a blog soon but have no coding know-how so I wanted to get advice from someone with experience.
Any help would be enormously appreciated!
safest online pharmacy viagra: euro pharmacy viagra – best drug store primer
9 Things Your Parents Taught You About Key Fob Repair Service key
What’s up to every body, it’s my first visit of this blog; this web site includes
amazing and genuinely fine material in support of readers.
Epivir-HBV: online pharmacy college – metoprolol people’s pharmacy
10 Things Everyone Hates About ADHD Diagnoses How Do You Diagnose Adhd In Adults
12 Companies Leading The Way In SEO Consultancy London seo Agency in london
There’s certainly a lot to learn about this issue.
I really like all the points you’ve made.
It’s in reality a nice and useful piece of info. I’m glad that you simply shared this useful information with us.
Please stay us informed like this. Thank you for sharing.
Medicament information leaflet. Brand names.
can i purchase cheap zofran pill
Some information about medicine. Read now.
warfarin monitoring pharmacy: online pharmacy delivery usa – mexico pharmacy adipex
best online pharmacy to buy viagra: detrol la online pharmacy – us viagra online pharmacy
best pharmacy to buy provigil: meijer pharmacy amoxicillin – navarro discount pharmacy store locator
Regards! Valuable stuff.
10 ADHD Adults Test Tricks All Experts Recommend
how to get tested for adhd (Dorthy)
brand viagra: uk pharmacy propecia – dostinex online pharmacy
buy pharmacy: gabapentin online pharmacy – 365 pharmacy kamagra
Undeniably believe that which you stated. Your favorite justification seemed to be
on the web the easiest thing to be aware of. I say to
you, I definitely get annoyed while people consider worries that they
plainly do not know about. You managed to hit the nail upon the top
and defined out the whole thing without having side effect , people can take a signal.
Will likely be back to get more. Thanks
15 Surprising Stats About How Much Are Spare Car Keys spare Key Car
konteyner fiyatları | Yazınız, konuyu oldukça geniş bir perspektiften ele almış. Özellikle verdiğiniz stratejik tavsiyeler benim için çok değerliydi.
Asking questions are really pleasant thing if you are
not understanding anything entirely, however this post offers pleasant understanding even.
Hi, I would like to subscribe for this web site to obtain hottest updates, so where can i do it please help out. https://gratisafhalen.be/author/columbustem/
konteyner fiyatları | Bu yazı, konuyu oldukça geniş bir perspektiften ele almış. Özellikle teknik detaylara verdiğiniz önem beni etkiledi.
cipro online pharmacy: nexium 40 mg pharmacy – viagra professional online pharmacy
generic viagra online pharmacy no prescription: cialis online review online pharmacy – inhouse pharmacy baclofen
Good post. I learn something totally new and challenging on websites I stumbleupon everyday.
It will always be useful to read through articles from other writers and use a little something from their sites.
viagra pharmacy india: percocet overseas pharmacy – pharmacy discount
What’s up it’s me, I am also visiting this site regularly, this
web page is in fact good and the people are actually sharing good thoughts.