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
Presenting our selection of Short Bedtime Stories, a charming trip through children’s dreams. Every narrative is skillfully written to enthrall young readers and transport them to fantastical settings and touching tales. These are the ideal stories to curl up with when the lights go off if you want to conclude the day with amazement and happiness.
Are You Responsible For An Upvc Window Repairs Budget? 12 Best Ways
To Spend Your Money window Repair
cipro online no prescription in the usa: cipro generic – ciprofloxacin order online
15 Lessons Your Boss Wishes You Knew About Waitrose Groceries Online Shopping Uk high-capacity Aluminum reel (vimeo.Com)
Could Lg Side By Side Fridge Freezer Be The Key To 2023’s Resolving?
short side by side fridge freezer, Ronnie,
Looking Into The Future What’s The Window Repair Industry Look Like In 10 Years?
double glazed window repairs near me
Tips For Explaining Treadmills Sale To Your Boss treadmill sale (Anya)
Is Private Diagnosis ADHD As Important As Everyone Says?
Adult adhd private assessment
The No. 1 Question Everybody Working In Become A Rep Avon Should Know How To Answer become An avon Representative
7 Simple Strategies To Completely Rocking Your Motorcycle
Accident Compensation motorcycle accident law firm
https://nolvadex.life/# where to buy nolvadex
buy cipro online canada ciprofloxacin 500mg buy online cipro pharmacy
http://lisinopril.network/# lisinopril 10 mg over the counter
Reenergized
4434 Pacific Coast Hwy,
ᒪong Beach, ᏟA 90804, United Stateѕ
562-689-9888
Wavi brain scan test accuracy
Hello! I could have sworn I’ve visited this website before
but after looking at some of the posts I realized it’s new to
me. Anyways, I’m certainly happy I stumbled upon it and I’ll be bookmarking it and checking back
often!
Why We Love Drive Folding Scooter (And You Should Too!)
heavy duty foldable mobility scooter – promarket.in.ua –
9 Things Your Parents Taught You About Adhd In Adult Women Symptoms
adhd in adult Women symptoms
buy cytotec pills online cheap: Misoprostol 200 mg buy online – cytotec abortion pill
buy cytotec over the counter buy cytotec online cytotec pills buy online
http://finasteride.store/# get cheap propecia without dr prescription
Why Nobody Cares About Shopping Online Uk Ski Plexi Scraper
An Easy-To-Follow Guide To Choosing Your Private ADHD Assessment how
to get a private Adhd assessment (https://rossoneri.ge)
A Provocative Rant About Adhd Symptoms For Women adhd In adults symptoms and Treatment
http://nolvadex.life/# what is tamoxifen used for
The 10 Most Scariest Things About Link Login Gotogel Link Login gotogel
The Reasons 4 Wheel Mobility Scooter Electric Scooters Is
Everyone’s Obsession In 2023 Electric Mobility Scooter
https://finasteride.store/# cost generic propecia online
cheap propecia prices cheap propecia prices cost cheap propecia no prescription
Uk Online Phone Shopping Sites: The Good, The Bad, And The Ugly Wowkk Tub Faucet
20 Fun Informational Facts About Counter Strike Pc case glove – Privatehd.org –