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
http://cytotec.club/# cytotec pills buy online
The 10 Most Scariest Things About Online Shopping Stores List microsoft rf keyboard and mouse
Guide To Cost Of Private ADHD Assessment UK:
The Intermediate Guide For Cost Of Private ADHD Assessment UK
cost of private adhd assessment uk
The Secret Secrets Of Double Glazed Units Near Me replacement double Glazed units near me
best value pharmacy
This Is The Ultimate Guide To Auto Folding Mobility Scooters Uk folding electric Scooter for adults
The 3 Biggest Disasters In Designer Handbags Brown The Designer Handbags
Brown’s 3 Biggest Disasters In History Designer Handbags Women’s (http://Www.Stes.Tyc.Edu.Tw)
https://lisinopril.network/# buy lisinopril 2.5 mg online
7 Simple Secrets To Totally You Into Locksmiths Car cheap car locksmiths Near me
cost cheap propecia no prescription: cost of cheap propecia without rx – buy generic propecia no prescription
5 Automobile Locksmith Tips You Must Know About For
2023 cheap auto locksmith (https://schultz-pappas.technetbloggers.de/)
See What Adhd Medication List Tricks The Celebs Are Utilizing Adhd Medication List
10 Citroen Key Replacement That Are Unexpected citroen C1 key replacement price (http://Www.cheaperseeker.com)
order propecia: cost propecia online – cost propecia online
Double Glazing Repairs Near Me Tools To Help You Manage Your Daily Lifethe One Double Glazing Repairs Near Me Technique Every
Person Needs To Learn Double glazing repairs Near me (Tntech.kr)
What Is Everyone Talking About Glass Repair Leeds Right Now
window hinges leeds
Best Vibrator For Clit Tools To Ease Your Everyday Lifethe Only Best Vibrator For Clit Trick
That Everyone Should Know best vibrator for clit
online pharmacy lisinopril lisinopril prescription cost lisinopril 20 mg tabs
where to buy nolvadex: tamoxifen and grapefruit – nolvadex only pct
Why We Are In Love With Espresso Machine Sale (And You Should Also!) home espresso machine
15 Things You’ve Never Known About Window Repair Near window Repairs
buy misoprostol over the counter: cytotec abortion pill – buy cytotec in usa
buy cytotec online cytotec pills buy online buy cytotec
What Is The Future Of Double Glazing Window Repairs Near Me Be Like In 100 Years?
replacement double glazed glass only (http://bitetheass.com/user/sprucemaple8/)
Here’s A Little-Known Fact Regarding Adult Female Adhd Symptoms
common symptoms of Adhd In females
https://lisinopril.network/# lisinopril 20mg coupon
This Is The Desk Treadmill Case Study You’ll Never Forget small under Desk treadmill
where can i buy cipro online ciprofloxacin 500 mg tablet price where can i buy cipro online
20 Trailblazers Lead The Way In Double Glazing In Leeds replacement
propecia buy cost of generic propecia cost generic propecia