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 Three Greatest Moments In Tommy Hilfiger And
Bags History tommy Hilfiger Bags usa
10 Facts About Birth Defect Lawsuit That Insists On Putting You In An Optimistic Mood Vimeo
Kamagra 100mg buy kamagra online sildenafil oral jelly 100mg kamagra
The No. Question That Everyone In Semi Truck Must Know How To Answer elk city Semi truck accident lawyer
11 Ways To Completely Revamp Your Washing Machine 12kg Price
washing machine 12kg Price
http://viagras.online/# buy Viagra over the counter
What’s The Current Job Market For Window Doctor Near
Me Professionals Like? Window Doctor Near Me (Dermandar.Com)
https://levitrav.store/# Levitra 20 mg for sale
5 Joker123 Gaming Instructions From The Pros spam
Buy Cialis online: Cialis 20mg price in USA – Cheap Cialis
Asking questions are in fact pleasant thing if you are not understanding
anything entirely, except this piece of writing
provides nice understanding even.
My brother recommended I might like this website. He was totally right. This post actually made my day. You can not imagine simply how much time I had spent for this information! Thanks!
I was recommended this blog by my cousin. I’m not sure
whether this post is written by him as no one else
know such detailed about my problem. You’re incredible! Thanks!
http://levitrav.store/# Buy Vardenafil 20mg online
16 Must-Follow Instagram Pages For CSGO New Case-Related Businesses case clutch (http://www.jzq5.cn/Space-uid-205043.html)
Tips For Explaining Autolocksmiths To Your Boss Locksmith Auto keys
20 Up-Andcomers To Watch The Bunk Bed Price Uk Industry bunk beds stores (Claude)
This Is A Folding Scooters Success Story You’ll Never Be
Able To Foldable Mobility Scooter With Spacious Seat
And Extended Range (http://Www.Mymobilityscooters.Uk)
Why Everyone Is Talking About Medical Malpractice Lawyer Right Now Vimeo.Com
https://cialist.pro/# Cialis 20mg price in USA
sildenafil oral jelly 100mg kamagra kamagra pills buy kamagra online usa
https://natumbe.ru
Hey there this is somewhat of off topic but
I was wondering if blogs use WYSIWYG editors or if you have
to manually code with HTML. I’m starting a blog soon but have no coding
experience so I wanted to get advice from someone with experience.
Any help would be greatly appreciated!
buy kamagra online usa cheap kamagra Kamagra Oral Jelly
cheapest cialis: buy cialis overseas – Buy Tadalafil 10mg
Kamagra 100mg price: kamagra pills – super kamagra
Buy Cenforce 100mg Online: order cenforce – cheapest cenforce
12 Facts About Upvc Repairs That Will Make You Think Twice About The Cooler.
Cooler upvc Windows repair near me
http://viagras.online/# sildenafil over the counter
Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how can we communicate?
buy Viagra over the counter: Buy Viagra online – Generic Viagra online