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
Indian pharmacy that ships to usa: Indian Cert Pharm – Online pharmacy
safe canadian pharmacy: best rated canadian pharmacy – cheapest pharmacy canada
My blog post – Explore Daycares Locations
купить диплом в ярославле
Indian Cert Pharm: Online pharmacy – IndianCertPharm
How To Find The Perfect Asbestos Attorney Lawyer Mesothelioma On The Internet mesothelioma Lawsuit
вывод из запоя стационар вывод из запоя стационар .
indian pharmacy: Indian Cert Pharm – Indian Cert Pharm
mexican rx online: Mexican Easy Pharm – Mexican Easy Pharm
https://naveridbuy.exblog.jp/37091064/
https://mexicaneasypharm.com/# buying from online mexican pharmacy
canadian pharmacy phone number
Mat6tube I enjoy your site, but you might want to review the spelling in a few posts. Some of them have noticeable spelling issues, which I find slightly irritating, though I’ll certainly return again.
Your post on gutter cleaning is super helpful! I will share it with my neighbors in Amherst Gutter Cleaning Service
I’ll immediately take hold of your rss as I can’t in finding your e-mail subscription hyperlink or e-newsletter service.
Do you’ve any? Kindly allow me know in order that I
could subscribe. Thanks.
https://candid-lion-dd3cm3.mystrikingly.com/blog/b0cea8a979f
Interesting perspective on how clean gutters contribute towards energy efficiency too!!! # # a n y K e y w o r Gutter Cleaning
https://naveridbuy.exblog.jp/37091323/
legitimate canadian pharmacy online: Canadian Md Pharm – canadian world pharmacy
https://naveridbuy.blogspot.com/2024/11/5_28.html
Medicament information. Long-Term Effects.
where to buy cheap tadacip prices
Actual what you want to know about drug. Read now.
https://naveridbuy.exblog.jp/37091263/
pet meds without vet prescription canada: Canadian Md Pharm – canadian pharmacy tampa
My brother suggested I might like this website. He was totally right.
This post actually made my day. You cann’t imagine simply how
much time I had spent for this information! Thanks! http://www.novello.jp/liner/joyful/joyful.cgi
I blog quite often and I seriously thank you for your information. Your article has truly peaked my interest.
I am going to take a note of your website and keep checking
for new information about once per week. I subscribed to your Feed too.
The 10 Most Scariest Things About Best Brand For Refrigerator good fridge brand (Theflatearth.win)
id=”firstHeading” class=”firstHeading mw-first-heading”>Search results
Help
English
Tools
Tools
move to sidebar hide
Actions
General
My brother recommended I might like this website. He was once entirely right.
This publish actually made my day. You cann’t
believe just how much time I had spent for this information! Thank you!
Best online Indian pharmacy: Indian Cert Pharm – Indian pharmacy that ships to usa
Best Indian pharmacy: indian pharmacy – Indian Cert Pharm
Mexican Easy Pharm: medication from mexico pharmacy – Mexican Easy Pharm