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://mexicoph24.life/# buying prescription drugs in mexico
10 Facts About Leeds Door Panels That Will
Instantly Get You Into A Great Mood Upvc Window Hinge Repairs Leeds
(https://King-Wifi.Win/Wiki/14_Smart_Ways_To_Spend_Your_Leftover_Windows_Leeds_Budget)
14 Misconceptions Commonly Held About American Style Fridge Freezer Fridge And Freezer
cheapest online pharmacy india: indian pharmacy fast delivery – indian pharmacy paypal
14 Cartoons About Replacement Car Key Costs To Brighten Your Day new car Key replacement
Guide To Multi Fuel Stoves Near Me: The Intermediate Guide To Multi
Fuel Stoves Near Me multi Fuel stove
Best Automatic Folding Mobility Scooter Uk Tips To Relax Your Daily
Life Best Automatic Folding Mobility Scooter Uk Trick That Should Be Used By Everyone
Know best automatic folding Mobility scooter Uk
Unexpected Business Strategies That Aided Double Glazed Windows Repair
Near Me To Succeed Double Glazing Windows Handles
All-Inclusive Guide To Double Glazed Window Repair window repairs near me
mexico pharmacies prescription drugs: Mexican Pharmacy Online – п»їbest mexican online pharmacies
A Vibrant Rant About Best Heat Pump Tumble Dryer heat-pump tumble dryer (https://telegra.ph/It-Is-The-History-Of-Tumble-Dryer-With-Heat-Pump-03-16)
You’ll Never Be Able To Figure Out This Rabbit
Vibrators Sex Toys’s Benefits rabbit vibrators sex Toys
10 Myths Your Boss Has Regarding Cheap Washing Machine 10kg cheap washing machine 10kg (Jenifer)
What To Look For In The Veleco That’s Right For You veleco mobility
(Theron)
hq pharmacy online 365
10 Things That Everyone Is Misinformed About Repair Window double glazed window repairs near me
pharmacies in mexico that ship to usa: cheapest mexico drugs – mexico drug stores pharmacies
9 Things Your Parents Teach You About Upvc Window Repairs Near Me Upvc Window Repairs Near Me
Five Killer Quora Answers To Handbag Tommy Hilfiger Tommy hilfiger bag Men
15 Reasons To Not Ignore Washer & Dryer Combo
washer & dryer combo
The Ultimate Glossary Of Terms About Repair Upvc Window upvc window repair, https://cochrane-palm-2.Blogbright.Net/,
Mesothelioma Lawyer Explained In Less Than 140 Characters mesothelioma Litigation
Guide To Walking Pad Standing Desk: The Intermediate Guide The
Steps To Walking Pad Standing Desk Walking pad standing desk
The Secret Secrets Of Window Repair Near upvc window repair
I’m not sure why but this blog is loading very slow for me.
Is anyone else having this issue or is it a problem on my end?
I’ll check back later and see if the problem still exists.
10 Real Reasons People Dislike Replacement Car Key Cost Replacement Car Key Cost cost of
replacement car keys – mehmetnuriarslan.com –
generic for prinivil
Why What CSGO Cases To Invest In Is Still Relevant In 2023 bravo case (images.google.Cg)
top 10 pharmacies in india http://indiaph24.store/# mail order pharmacy india
п»їlegitimate online pharmacies india