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
lisinopril 10 mg price in india lisinopril 200mg lisinopril 5 mg
Could Mesothelioma Legal Question Be The Key To 2023’s Resolving?
asbestos claim
See What Birth Defect Lawsuit Tricks The Celebs Are Making Use Of birth defect
A List Of Common Errors That People Make With Cheapest Folding Mobility Scooter best folding Mobility Scooter
Ten Mobile Car Locksmiths That Really Make Your Life
Better mobile car locksmiths
9 Lessons Your Parents Taught You About Veterans Disability Lawsuit Veterans Disability lawsuit
lisinopril cost 5mg zestoretic generic price of zestril
15 Presents For The Selling Beauty Products From Home Uk Lover In Your Life sell avon cosmetics (Web011.dmonster.kr)
http://ciprofloxacin.tech/# ciprofloxacin over the counter
Воєнторг
19. Тактические ремни и пояса для фиксации снаряжения
куртка зимова хелікон https://voentorgklyp.kiev.ua/odyag/kurtky/kurtky-helikon-tex/ .
https://cytotec.club/# Abortion pills online
The 10 Scariest Things About Mental Health Assessment Near
Me mental health assessment Near me
Highly descriptive post, I liked that a lot. Will there be a part 2?
What’s up, its good post regarding media print, we all understand media is a wonderful source of facts.
What Do You Need To Know To Be Are Ready For Double Glazing Windows Leeds
upvc door repairs roundhay (Doodleordie.com)
The Reasons You’re Not Successing At Bentley Key Fob bentley car keys
10 Facts About Car Key Replacement Service Near
Me That Can Instantly Put You In An Optimistic Mood remote car key replacement – Danial –
Nine Things That Your Parent Taught You About Mini Sectional Sofa Mini sectional sofa
This design is wicked! You definitely know how to keep a reader amused.
Between your wit and your videos, I was almost moved to start
my own blog (well, almost…HaHa!) Fantastic job. I really loved what you had to say, and more than that, how you presented it.
Too cool!
Your method of explaining everything in this paragraph is truly
nice, every one be able to without difficulty know it, Thanks a lot https://t.me/s/ventilation_terrebonne
http://lisinopril.network/# prinivil generic
What’s The Job Market For Double Glazed Repairs Near Me Professionals Like?
double glazed repairs Near me
Three Common Reasons Your Slot Strategies Isn’t Working (And How To Fix It) slot payouts (m.tjtechno.Co.Kr)
A Provocative Remark About Upvc Window Repairs Window Repairs Near Me
https://finasteride.store/# propecia no prescription
https://cytotec.club/# buy cytotec
Search Engine Optimisation Services Tools To Improve Your Everyday Lifethe Only Search Engine Optimisation Services Trick That Everybody Should Learn Search engine optimisation services
What’s Everyone Talking About Double Glazing London Today nearby
“The Ultimate Cheat Sheet On Backlink Seo Software backlink Submission software
http://finasteride.store/# get generic propecia without rx