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
9 Things Your Parents Taught You About Replace Window Handles replace window handles
Your Family Will Be Grateful For Getting This Work From Home Jobs Part
Time Work From Home Jobs Near Me
The Personal Injury Litigation Case Study You’ll Never Forget personal injury law Firms
You have made some good points there. I checked on the internet for
additional information about the issue and found most people will go along with your views on this web site.
Three Greatest Moments In CSGO Cases New History Operation Broken Fang
Case; https://Maps.Google.Com.Ua/Url?Q=Https://Hartley-Hester.Federatedjournals.Com/11-Ways-To-Completely-Sabotage-Your-Counter-Strike-2,
cytotec pills buy online buy cytotec online fast delivery cytotec abortion pill
Mesothelioma Litigation Tools To Facilitate Your Day-To-Day Life asbestos case (Xiomara)
https://cytotec.club/# cytotec abortion pill
Why Everyone Is Talking About Combination Washer Dryer In One Right Now Best Washer Dryer (http://Www.Stes.Tyc.Edu.Tw/)
what is tamoxifen used for tamoxifen pill does tamoxifen make you tired
15 Trends That Are Coming Up About Tier 2
Seo tiered link building strategy
Where Can You Find The Most Effective Heat Pump Tumble Dryer Uk Information?
heat pump tumble dryer reviews
A Look At The Secrets Of Window Repair Near Window Repair Near me
9 . What Your Parents Taught You About Boat
Accident Lawsuit Boat Accident lawsuit
You’ll Be Unable To Guess Small Treadmill With Incline’s
Tricks small treadmill With incline
cipro 500mg best prices buy cipro online without prescription buy cipro cheap
http://finasteride.store/# cost generic propecia price
order cheap propecia pill: cost propecia no prescription – buying generic propecia no prescription
Methaqualone, like Quaaludes, is a prescription sedative that
increases the activity of GABA (neurotransmitter) receptors.
https://ciprofloxacin.tech/# buy cipro online canada
http://lisinopril.network/# lisinopril 3760
25 Unexpected Facts About Double Memory Foam Mattresses double Mattress in sale
http://cytotec.club/# Cytotec 200mcg price
ciprofloxacin 500mg buy online buy cipro online ciprofloxacin generic
mexican pharmacy weight loss
14 Cartoons About Hiring Car Accident Attorneys That’ll Brighten Your Day houston car accident
attorney (fuglsang-steen.blogbright.net)
Good post. I learn something totally new and challenging on websites I stumbleupon everyday.
It’s always helpful to read through articles from other writers and practice a little something from their sites.
This Is The Advanced Guide To Defra Exempt Stoves DEFRA registered stoves
5 Double Glazed Windows Repairs Lessons From The Pros service
zestril 10mg lisinopril 2.5 cost lisinopril 40 mg no prescription