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
The 9 Things Your Parents Teach You About Folding Treadmills UK Folding Treadmills
uk (Cwit.edu.sa)
http://ciprofloxacin.tech/# cipro for sale
The Top Companies Not To Be In The Lawyers That Handle Mesothelioma Cases Industry texas mesothelioma lawyer
cost of cheap propecia without a prescription: cheap propecia without insurance – buy cheap propecia pills
Five Killer Quora Answers To Standing Desk Treadmill
standing desk treadmill – Pedro,
What You Should Be Focusing On Improving Designer Handbags Large Designer handbags Cheap
Good post! We are linking to this particularly great post on our site.
Keep up the good writing.
Review my website – silver medals
5 Laws That Will Help With The Car Accident Litigation Industry car Accident attorneys
Indisputable Proof That You Need Michael Kors Bag Michael kors Handbags grey
Abortion pills online: order cytotec online – buy cytotec online
https://cytotec.club/# buy cytotec over the counter
Check Out The Repair Window Tricks That The Celebs Are Using Double Glazed window repairs Near me
How To Create An Awesome Instagram Video About Double Glazing
Doctor Near Me double glazing replacement window – Linnie,
cipro 500mg best prices ciprofloxacin 500 mg tablet price buy generic ciprofloxacin
10 Things You Learned In Kindergarden That’ll Help
You With SEO Management Services Seo services manchester
14 Smart Ways To Spend Leftover Malpractice Attorney Budget malpractice lawsuits
(Kristal)
http://nolvadex.life/# tamoxifen headache
Guide To Mazda Replacement Keys: The Intermediate Guide Towards Mazda
Replacement Keys mazda replacement Keys
Магазин, где можно выбрать раковина villeroy boch o novo под любой интерьер.
http://cytotec.club/# buy cytotec pills online cheap
Your mode of explaining all in this piece of writing is in fact fastidious, all be able to easily know it, Thanks a lot https://fenestrationdessommets.com/en/products/windows/?amp=1
10 Things We All Hate About Avon Book Online current
avon book; Sycw1388.co.kr,
ciprofloxacin: cipro ciprofloxacin – where can i buy cipro online
Hi there to every one, for the reason that I am
truly keen of reading this web site’s post to be updated
regularly. It contains fastidious material.
If you are going for finest contents like I do,
simply visit this web page all the time for the reason that
it offers feature contents, thanks
Remarkable issues here. I am very happy to look your article.
Thanks so much and I’m having a look forward to touch you.
Will you please drop me a e-mail?
14 Questions You Might Be Anxious To Ask Injury Law injury law firm (littleyaksa.yodev.net)
ciprofloxacin generic ciprofloxacin order online antibiotics cipro
The 10 Scariest Things About High Roller Slots high roller slots – 125.141.133.9,
tamoxifen vs raloxifene: tamoxifen rash pictures – tamoxifen endometrium