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
tamoxifen and depression: tamoxifen rash – tamoxifen vs raloxifene
“Ask Me Anything”: Ten Answers To Your Questions About Shopping Online Uk Vimeo.Com
See What Replacement Nissan Key Fob Tricks The Celebs Are Making Use Of replacement nissan key fob; Jina,
https://lisinopril.network/# drug prices lisinopril
10 Of The Top Mobile Apps To Mesothelioma mesothelioma Law
Here is my web-site; facebook for hooking up
ciprofloxacin generic: buy cipro – buy generic ciprofloxacin
cytotec buy online usa buy cytotec over the counter buy cytotec over the counter
https://lisinopril.network/# lisinopril 5 mg price
http://cytotec.club/# purchase cytotec
Why You Should Be Working With This CSGO Cases Opening
Sites case opening
A New Trend In CS GO Weapon Case Operation breakout Weapon case
Are You Able To Research Audi A1 Key Replacement Online replacement audi key
fob (http://extension.unimagdalena.edu.co/extension/lists/contactenos/dispform.aspx?id=1179118)
Why Everyone Is Talking About Ghost Immobiliser Near Me This Moment
ghost 2 Immobiliser cost
buying generic propecia without dr prescription: buying generic propecia without insurance – cost propecia without dr prescription
Five Qualities That People Search For In Every Window Repair Near Upvc window repair near me
What’s The Current Job Market For Search Engine Optimization Services
Professionals Like? search engine optimization Services
https://finasteride.store/# buying generic propecia without dr prescription
how does tamoxifen work tamoxifen 20 mg tablet benefits of tamoxifen
How To Save Money On Efoldi Mobility Scooter For Sale veleco
mobility scooter for sale; Rentry.co,
See What Veleco Mobility Scooter For Sale Tricks The Celebs
Are Utilizing veleco Mobility scooter for sale
orator72
http://cytotec.club/# purchase cytotec
10 Facts About Treadmill Folding Incline That Will Instantly Put You In A Positive Mood small Treadmill incline, http://Www.huenhue.Net,
https://cytotec.club/# buy cytotec online
Guide To Double Glazed Units Near Me: The Intermediate Guide In Double Glazed Units
Near Me double glazed Units Near me
https://ciprofloxacin.tech/# buy cipro online
20 Things You Need To Know About Will CSGO Cases Go Up In Price Case Falchion
Excellent weblog right here! Also your site lots up fast!
What web host are you the use of? Can I get your associate hyperlink in your host?
I want my web site loaded up as fast as yours lol
lisinopril sale: lisinopril tablets india – lisinopril 20 mg for sale