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
10 Life Lessons That We Can Learn From Double Glazed Units Near Me replacement double Glazed units near me
Visit my website: craiglistforsex
The Reasons To Focus On Improving Jaguar Keys Replacement Jaguar key replacement cost
5 Clarifications Regarding Best Online Shopping Sites Clothes 4
Pair Baby Socks Set – https://Vimeo.com/,
Ten Sweet Bonanza Xmas Slot Demo That Will Actually Make Your Life Better Main akun demo Slot sweet bonanza
Very nice post. I just stumbled upon your weblog and wanted to say that I have truly enjoyed surfing around your blog posts.
In any case I’ll be subscribing to your feed and I hope you write again soon!
Why Interactive Slots Is Still Relevant In 2023 Mobile slots
Get To Know You The Steve Jobs Of The Car Key Lockout Service Industry Key
unlock service (https://losguerrerosdeoracion.com/)
Os melhores jogos deste cassino Blaze
11 Methods To Completely Defeat Your Best Slots Modern Slots (http://Www.Wakewiki.De)
Medicament information. What side effects can this medication cause?
can i get trileptal online
Best information about medication. Read now.
“The Railroad Injuries Litigation Awards: The Top, Worst, Or Weirdest Things We’ve Ever Seen railroad Injuries lawyer
Cheapest Online Grocery Shopping Uk: What No One Is Talking
About Exterior Construction Glue
The 12 Most Unpleasant Types Of High Limit Slots Accounts You Follow On Twitter Evoplay Slots Payouts
What Freud Can Teach Us About Novice Slots demo Slots – http://www.tradwicca.hu –
Helpful knowledge Kudos.
Could Trusted Online Shopping Sites For Clothes Be The Key To 2023’s Resolving?
fall arrest d-Ring connectors
Ten Slot Demos That Really Change Your Life slot demos
Solutions To The Problems Of Online Famous Shopping Sites vimeo.Com
20 Reasons Why Ai To Rewrite Text Will Not Be Forgotten ai article
rewriter (Pete)
There is definately a great deal to find out about this issue.
I love all the points you have made.
The Ultimate Guide To Novice Slots demo slots (Evelyn)
Ask Me Anything: 10 Responses To Your Questions About Slots For Fun Best Online
Slots (Qooh.Me)
20 Up-Andcomers To Watch The Medium Variance Slots Industry Evoplay slots fun
10 Things You’ve Learned From Kindergarden To Help You Get Started With Shopping Online Black Ethernet Cable 10 Feet
Learn About How To Ship To Ireland From Uk While Working From The Comfort Of Your Home Vimeo.Com
Does your site have a contact page? I’m having problems locating it but, I’d like to shoot
you an email. I’ve got some creative ideas for your blog
you might be interested in hearing. Either way, great website and I look
forward to seeing it expand over time.
Medicament information for patients. Generic Name.
proscar
Some trends of medicines. Read now.
The Best Advice You Can Ever Receive On Semi Truck Lawsuit semi truck accident lawyer
Thanks for finally writing about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Loved it!