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
where can i buy cheap prednisolone price
Узнайте стоимость диплома высшего и среднего образования и процесс получения
buy dapoxetine online: Priligy tablets – buy dapoxetine online
Your Family Will Thank You For Having This Four Wheel Rollator rollator Walker
Thanks , I have just been searching for information approximately
this subject for a long time and yours is the greatest I have found
out till now. But, what concerning the conclusion? Are you certain about the source?
Poker Tournament Tips 다바오4989 (dnhangwa2.Webmaker21.kr)
п»їplavix generic: PlavixClo – generic plavix
I am extremely impressed with your writing skills and also with the layout on your blog.
Is this a paid theme or did you customize it yourself?
Anyway keep up the excellent quality writing, it is rare to see a nice blog like this one these days.
stromectol tablets uk: minocycline 100mg tablets – minocycline foam
http://iverfast.com/# can you buy stromectol over the counter
I love that your site offers practical steps victims can take immediately after an accident—it’s so necessary!: # # a n y K e y w o r d Louisville car accident lawyer
amoxicillin discount coupon: antibiotic amoxicillin – amoxicillin without rx
Currently iit looks like Wordpreѕѕ is tһe t᧐p blogging platfom avɑilable rіght now.
(from what I’ve read) Is tһat whɑt you are using on your
blog?
my page; dede4d (https://bbarlock.com)
Lisinopril 1st buy Lisinopril 1st lisinopril1st
I have read some good stuff here. Definitely value bookmarking for revisiting.
I surprise how so much effort you set to create any such magnificent informative web site.
Have you ever thought about publishing an ebook or guest authoring on other websites?
I have a blog centered on the same information you discuss and would love to have you share some stories/information. I know my audience would
value your work. If you’re even remotely interested, feel free to shoot me an e-mail.
15 Interesting Facts About Pragmatic You’ve Never Seen 프라그마틱 무료슬롯 (http://Www.Google.Mn)
buy plavix: Clopidogrel Best Prices – plavix medication
id=”firstHeading” class=”firstHeading mw-first-heading”>Search results
Help
English
Tools
Tools
move to sidebar hide
Actions
General
Thank you for sharing your info. I truly appreciate your efforts and I will be waiting for
your further post thank you once again.
What’s up every one, here every person is sharing these familiarity, so it’s good
to read this website, and I used to pay a quick visit this weblog
everyday.
http://iverfast.com/# stromectol 12mg
Spa Therapy 오피커뮤니티
What Butt Plugs Shop Experts Want You To Learn butt dildos
Essas estruturas são famosas por serem sondadas e exploradas por criminosos.
My brother suggested I might like this website. He was entirely right.
This put up truly made my day. You can not believe simply
how so much time I had spent for this information! Thanks!
Please let me know if you’re looking for a writer for your
blog. You have some really great articles and I believe I would be
a good asset. If you ever want to take some of the load off, I’d really like to write some content for your blog in exchange for a link back
to mine. Please blast me an e-mail if interested.
Kudos!
Быстрое обучение и получение диплома магистра – возможно ли это?
how to buy generic clomid: clomid – can i order cheap clomid without a prescription
Официальная покупка диплома вуза с сокращенной программой обучения в Москве