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
ciprofloxacin 500mg buy online ciprofloxacin 500 mg tablet price ciprofloxacin mail online
The Reason Why Adding A Bunk With Double Bed To Your Life Can Make All The A Difference quadruple bed (Janis)
This Is The Ultimate Guide To Folding Treadmill training
10 Things You Learned In Preschool That’ll Help You Understand Local
Locksmith For Cars nearest locksmith For cars
10 Tips For Quickly Getting Ford Key Repair ford car key replacement near
me (Colin)
cost generic propecia pills order cheap propecia pill cost of generic propecia
15 Up-And-Coming Mesothelioma Law Bloggers You Need To Check Out mesothelioma case
(http://0522224528.Ussoft.kr/)
ciprofloxacin 500 mg tablet price: buy cipro online – buy cipro
ciprofloxacin 500 mg tablet price buy cipro cheap antibiotics cipro
If some one wishes to be updated with latest technologies therefore he must be pay a quick visit this site and be up to date daily.
antibiotics cipro buy generic ciprofloxacin ciprofloxacin
I needed to thank you for this good read!! I absolutely
enjoyed every bit of it. I have you book marked to look
at new stuff you post…
That is very fascinating, You are a very professional blogger.
I’ve joined your feed and sit up for searching for more of your great post.
Also, I have shared your website in my social networks
http://finasteride.store/# buy propecia for sale
Misted Double Glazing Repairs Tips That Can Change Your Life Door repair
5 Double Glazing Repairs Near Me Projects That Work
For Any Budget Replacement Double Glazed Glass Only
The 10 Scariest Things About Replacement Key
For Mini Cooper replacement Key for mini cooper
https://lisinopril.network/# where to buy lisinopril without prescription
15 Latest Trends And Trends In Under Counter Side By Side Fridge Freezer 90cm which brand has the best side by side refrigerator (Candida)
15 Surprising Facts About Bunk Beds For Kids cheap bunk beds for kids
You’ll Never Guess This Double Glazing Doors Repairs’s Secrets upvc door repairs Near me
I get pleasure from, cause I found exactly what I was taking a look for.
You’ve ended my 4 day long hunt! God Bless you man. Have a great
day. Bye
You’ll Never Be Able To Figure Out This Window Repair
Near’s Tricks window repair
What Is The Secret Life Of Locksmith Cars Nearest car locksmith
https://cytotec.club/# buy cytotec
tamoxifen chemo nolvadex vs clomid tamoxifen breast cancer
Nine Things That Your Parent Teach You About Mesothelioma Litigation mesothelioma litigation – Web011.dmonster.kr,
Don’t Be Enticed By These “Trends” About Locksmiths Near
Me Auto cheapest Auto locksmith
https://lisinopril.network/# lisinopril 40 mg brand name
10 Best Mobile Apps For ADHD Testing Adult Test For Adhd