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
Kamagra 100mg price kamagra.win Kamagra 100mg
Cenforce 100mg tablets for sale: Buy Cenforce 100mg Online – cenforce for sale
15 Up-And-Coming Motor Vehicle Attorney Bloggers You Need To Follow newport beach Motor vehicle accident law Firm
How To Get More Benefits Out Of Your Double Glazing Repair London fit
Buy generic Levitra online: Levitra online USA fast – Buy Vardenafil online
canadian pharmacy cialis
I’m extremely impressed with your writing talents and also with the
structure to your weblog. Is this a paid topic or did you customize it yourself?
Anyway keep up the nice high quality writing, it’s rare to peer a great weblog like this
one nowadays..
The 3 Biggest Disasters In Erb’s Palsy Attorney The Erb’s Palsy Attorney’s 3 Biggest Disasters In History tahlequah Erb’s palsy attorney
https://cialist.pro/# buy cialis pill
Guide To Link Daftar Gotogel: The Intermediate Guide For Link Daftar Gotogel link
daftar gotogel (tehnoplit.ru)
5 Slot Demo Pragmatic Projects For Every Budget demo slot pragmatic Maxwin
Ten Things You Learned In Kindergarden That Will Help You With Glimmersticks Eyeliner Avon True
Colour Glimmerstick Brow Definer (https://Highwave.Kr/Bbs/Board.Php?Bo_Table=Faq&Wr_Id=2075627)
It’s The One Can I Buy From A Uk Website Trick Every Person Should
Be Able To Ada Compliant Tub
10 Books To Read On Windows And Doors Leeds upvc window hinge repairs leeds
(Georgianna)
Cheap generic Viagra online Buy Viagra online Sildenafil 100mg price
cheapest cenforce Buy Cenforce 100mg Online Buy Cenforce 100mg Online
Привет всем! Я с удовольствием поделюсь своим опытом процесса на площадке “ап икс официальный сайт“. Это место, где страсть и забавы встречаются в захватывающем сочетании. Многообразие игр, благодарные бонусы и профессиональная поддержка делают геймплей опыт запоминающимся. Присоединяйтесь и ощутите волнение триумфа вместе с “UP X”!
Generic Tadalafil 20mg price: buy cialis overseas – buy cialis pill
10 Steps To Begin Your Own Best Online Shopping Sites Clothes Business Healthy Dog
Snacks – Leanne –
https://levitrav.store/# Levitra 10 mg buy online
Kamagra tablets: kamagra – super kamagra
hey there and thank you for your information – I’ve definitely picked
up anything new from right here. I did however expertise a few technical
issues using this site, since I experienced to reload the site a lot
of times previous to I could get it to load correctly. I had been wondering if your
hosting is OK? Not that I’m complaining, but sluggish loading instances times
will often affect your placement in google and could damage your high-quality score if advertising and
marketing with Adwords. Anyway I am adding
this RSS to my email and can look out for a lot more of your respective exciting content.
Ensure that you update this again very soon.
Cheap Cialis: cialist.pro – Cialis over the counter
Used 3 Wheel Mobility Scooters For Sale Tools To Ease Your Everyday
Lifethe Only Used 3 Wheel Mobility Scooters For Sale Trick Every Person Should Learn 3 wheel mobility scooter
You’ll Never Guess This 5kw Wood Burning Stove’s Secrets 5Kw Wood Burning Stove
http://kamagra.win/# Kamagra tablets
Купить квартиру в Казани https://novostroyzhilie.ru/ от застройщика. Планировки и цены трехкомнатных, двухкомнатных и однокомнатных квартир в новостройке.
magnificent post, very informative. I wonder
why the opposite experts of this sector don’t understand this.
You must continue your writing. I’m sure, you’ve a great readers’ base already!
Ten Trusted Online Shopping Sites For Clothes-Related Stumbling Blocks You Should Never Share On Twitter
central vacuum cleaner system
Vardenafil online prescription: levitrav.store – Cheap Levitra online