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
What Is Malpractice Claim And Why Is Everyone
Dissing It? Malpractice Lawsuit
Awesome! Its genuinely awesome paragraph, I have got much clear idea concerning from this piece
of writing.
The Worst Advice We’ve Heard About Slot Rewards top jackpot Slots
Lionel Messi https://lionelmessi.prostoprosport-br.com e um jogador de futebol argentino, atacante e capitao do clube da MLS Inter Miami. , capitao da selecao argentina. Campeao mundial, campeao sul-americano, vencedor da Finalissima, campeao olimpico. Considerado um dos melhores jogadores de futebol de todos os tempos.
See What Online Charity Shop Uk Clothes Tricks The Celebs Are Using online Charity shop Uk clothes
The Story Behind Lock Smith For Cars Will Haunt You For The Rest Of Your Life!
elsycrays.top
A Time-Travelling Journey: How People Talked About
Truck Accidents Attorney 20 Years Ago Truck Accident Law Firms
15 Shocking Facts About ADHD Diagnosis The Words You’ve Never
Learned get diagnosed for adhd
продвижение сайтов в москве яндекс prodvizhenie-sajtov-v-moskve115.ru .
продвижение сайтов по москве в топ prodvizhenie-sajtov-v-moskve115.ru .
Howdy! I know this is kind of off topic but I was wondering if you knew where
I could locate a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m
having trouble finding one? Thanks a lot!
A How-To Guide For Motor Vehicle Settlement
From Beginning To End Motor Vehicle Accidents
How Semi Truck Became The Top Trend In Social Media Semi truck accident law Firm
See What Where To Buy Electronics Online Tricks The Celebs Are
Making Use Of where to buy electronics Online
It’s a shame you don’t have a donate button! I’d without a doubt donate to this excellent blog!
I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google
account. I look forward to fresh updates and will share this website with my Facebook group.
Chat soon!
Also visit my web-site; the growth matrix reviews
11 “Faux Pas” That Are Actually OK To Make With Your Slot Games Popular online slots
25 Surprising Facts About Bonus Slots slot Volatility
10 Things Everybody Hates About Shopping Online Shopping
Online shopping online sites clothes (Velma)
Thanks for finally talking about > JS 依照元素出現數量排序 –
馬老師 雲端研究室 < Liked it!
Why You Should Concentrate On Improving Cerebral Palsy Law Cerebral Palsy Lawyer
Heya i am for the first time here. I found this board and I find It really useful & it helped me
out much. I hope to give something back and aid others like you helped me.
продвижение сайтов в москве тарифы prodvizhenie-sajtov-v-moskve115.ru .
Slots For Fun: The Ugly Truth About Slots For Fun Play Slots
создание и продвижение сайтов в москве я топ сайт http://prodvizhenie-sajtov-v-moskve115.ru/ .
продвижение сайтов в москве гугл https://prodvizhenie-sajtov-v-moskve115.ru .
How To Survive Your Boss On Birth Defect Legal birth Defect Law Firms
Here’s A Few Facts About Licensed Slots. Licensed Slots Themed slot games
Buying Online From Uk To Ireland Tools To Improve Your
Daily Lifethe One Buying Online From Uk To Ireland Trick Every
Individual Should Know buying online from uk to ireland (Willard)
seo продвижение сайтов в москве агентство http://prodvizhenie-sajtov-v-moskve115.ru/ .
order cheap advair diskus without insurance