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
cytotec abortion pill cytotec online cytotec buy online usa
https://finasteride.store/# cost generic propecia without dr prescription
buy cytotec in usa: cytotec abortion pill – buy cytotec online fast delivery
Watch Out: How Slot Games Is Taking Over And What You Can Do About It best online slots
It’s The Evolution Of Dangerous Drugs Law Firm dangerous drugs lawyers
http://lisinopril.network/# zestril 5 mg tablet
I wanted to thank you for this good read!! I absolutely loved every little bit of it.
I have got you bookmarked to look at new stuff you post…
You’ll Never Guess This Window Repair Near Me’s Secrets
window repair Near Me
This is my first time pay a visit at here and i am really impressed to read
all at single place.
http://lisinopril.network/# lisinopril tab 100mg
zestoretic online
A An Overview Of Kit Avon From Start To Finish
kit Avon
cost generic propecia without prescription: generic propecia without a prescription – get cheap propecia tablets
Hi, yеs this article іs actually pleasdɑnt and I have learned lot of thіngs from it on the topic of blogging.
thanks. http://wiki.edomex.cinvestav.mx/index.php/Ck4d_Link_Alternatif_Ck4d_Login:_Gimana_Dan_Di_Mana_Membeli_Kupon_Ck4d_Link_Alternatif_Ck4d_Login
In recent years, the use of https://greenacrecbdgummies.net products—derived from the hemp plant—has gained significant popularity due to their reported health benefits.
http://lisinopril.network/# zestril 20 mg tablet
5 Veterans Disability Lawsuit Projects That Work For Any Budget veterans disability law firm (http://Fpcom.co.kr/bbs/board.php?bo_table=free&wr_id=1312838)
You’re About To Expand Your Side By Side Freezers Fridge Options advanced Cooling technology
http://cytotec.club/# buy cytotec online
http://finasteride.store/# get cheap propecia
cost generic propecia tablets: cost generic propecia without insurance – cost of generic propecia without prescription
Do You Know How To Explain Jackpot Slots To Your Boss progressive jackpot slots
ciprofloxacin order online: ciprofloxacin 500 mg tablet price – ciprofloxacin generic
http://lisinopril.network/# lisinopril 25mg tablets
Work From Anywhere In The World Online: A Simple Definition part time work from home jobs
nolvadex gynecomastia: tamoxifen headache – nolvadex steroids
What’s The Job Market For Upvc Repairs Near Me Professionals?
upvc repairs near me
Also visit my website … ссылка на мегу
http://lisinopril.network/# zestril 30mg generic
Woodburner Stove Tips To Relax Your Daily Lifethe One Woodburner
Stove Technique Every Person Needs To Be Able To woodburner stove (Charley)