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
Five Things You Didn’t Know About Erb’s Palsy Case lawyer
After I originally commented I appear to have
clicked on the -Notify me when new comments are added- checkbox and from
now on every time a comment is added I recieve 4 emails with
the same comment. Is there a way you are able to remove
me from that service? Appreciate it!
Guide To Birth Injury Attorney: The Intermediate Guide On Birth Injury
Attorney Birth Injury Attorney
The Companies That Are The Least Well-Known To Monitor In The Medical Malpractice Law Industry Beaufort medical malpractice attorney
Double Glazing Windows Leeds Tips From The Top In The
Industry double glazing door repairs
cheapest viagra sildenafil over the counter Viagra without a doctor prescription Canada
Levitra generic best price: Buy generic Levitra online – Vardenafil buy online
10 Things We All Hate About Planet Spa Beauty Sleep Planet Spa (Littleyaksa.Yodev.Net)
Tadalafil Tablet: Cialis 20mg price in USA – buy cialis pill
https://viagras.online/# Viagra online price
buy Kamagra: kamagra pills – Kamagra 100mg
https://viagras.online/# order viagra
A Avon Planet Spa Pillow Mist Success Story You’ll Never Be Able To avon planet spa pillow mist (Philip)
What’s The Job Market For Double Glazed Window Repairs Professionals?
Double Glazed Window Repairs
buy kamagra online usa kamagra oral jelly super kamagra
15 Gifts For The Online Shopping Uk Lover In Your Life Bulk Usb Drives 4Gb
http://cenforce.pro/# cenforce.pro
The 9 Things Your Parents Teach You About Window Repair Window repair
I have to thаnk you for the efforts you’ve put in penning
this site. I’m hߋping to view the same high-grade content
by you later on as well. Ιn truth, your crеative writing abilities has encouraged me to get my
own, personal site now 😉
My web blog – ASocks promotional code
Vardenafil online prescription levitrav.store Vardenafil price
11 Strategies To Refresh Your Railroad Injuries Lawsuit
creve coeur railroad injuries lawyer
When I initially commented I clicked the “Notify me when new comments are added”
checkbox and now each time a comment is added I get four emails with the same comment.
Is there any way you can remove me from that service?
Appreciate it!
Generic Cialis without a doctor prescription: buy cialis online – Generic Tadalafil 20mg price
7 Tips To Make The Most Out Of Your Mental Health Assessment How to get mental health Assessment
The 10 Scariest Things About Designer Handbags Black Bag
The 10 Most Scariest Things About Double Glazing Repairs Near Me Double glazing Repairs
https://cialist.pro/# Cialis without a doctor prescription
Cheapest Sildenafil online viagras.online Viagra without a doctor prescription Canada
See What Spare Car Keys Cut Tricks The Celebs Are Making Use Of spare car keys Cut
Viagra tablet online: Cheapest place to buy Viagra – buy Viagra online