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
CS GO Cases Opening: What’s No One Is Talking About Weapon Case (https://Maps.Google.Nr/Url?Q=Https://Imoodle.Win/Wiki/A_StepByStep_Guide_To_CSGO_New_Cases_From_Start_To_Finish)
You actually reported that really well!
Here is my page: https://staceyembracingchange.com/
Double Bunk Tools To Ease Your Daily Life Double Bunk Technique Every Person Needs To
Know double bed Double bunk (https://ocelotlace0.bravejournal.net)
tamoxifen hip pain does tamoxifen cause joint pain tamoxifen for sale
9 . What Your Parents Taught You About Upvc Window Repairs
Near Me Upvc window repairs near me
Five Killer Quora Answers To Fridge Freezer For Sale fridge freezer for Sale
http://finasteride.store/# buying propecia without prescription
Five People You Must Know In The Small Double Mattresses Industry double mattress sale uk
cipro ciprofloxacin where can i buy cipro online buy cipro online without prescription
lisinopril 4 mg: lisinopril 25 mg cost – buy lisinopril uk
Ghost Installations Tips That Will Change Your Life
ghost installations, Eusebia,
Bunk Bed Online Store Tools To Ease Your Daily Lifethe One Bunk Bed Online
Store Trick That Every Person Should Be Able To bunk Bed online
What The 10 Most Worst Bean Coffee Maker FAILURES Of All Time Could Have Been Prevented bean to cup machines (Adolph)
happy family store
Guide To Online Shopping Sites In United Kingdom: The Intermediate Guide In Online
Shopping Sites In United Kingdom online shopping sites In united kingdom
ciprofloxacin 500 mg tablet price cipro online no prescription in the usa buy cipro online
14 Businesses Doing An Amazing Job At CSGO Cases Highest
Roi cs20 case – quietmona.Com,
See What Retro Espresso Machine Tricks The Celebs Are Making Use Of retro espresso Machine
Hey there I am so delighted I found your website, I really found
you by mistake, while I was looking on Bing
for something else, Nonetheless I am here now and would just
like to say thanks for a fantastic post and a all round enjoyable blog (I also
love the theme/design), I don’t have time to browse it all at
the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to
read more, Please do keep up the fantastic work.
Interesting blog! Is your theme custom made or did you download it
from somewhere? A theme like yours with a few simple tweeks
would really make my blog stand out. Please let me know where you got your theme.
Bless you
Here is my web page; 실시간중계
Why You Should Concentrate On Making Improvements To Repairing Upvc Windows Near By
Titration ADHD Meds Tools To Ease Your Everyday Lifethe Only Titration ADHD Meds Trick
That Should Be Used By Everyone Know titration
Five Things You Didn’t Know About Ford Replacement Key Programming replacement keys for Ford (http://lineyka.org/)
lisinopril brand name australia: buy lisinopril 20 mg without prescription – lisinopril 10 mg online no prescription
3 Common Causes For Why Your Keys For Mercedes Isn’t Working (And Solutions
To Resolve It) Mercedes Replacement Keys
20 Things You Must Know About Designer Bags Affordable Designer Handbags Uk
5 Killer Quora Answers To Psychiatry Assessment Uk Psychiatry Assessment Uk
antibiotics cipro: ciprofloxacin mail online – cipro 500mg best prices
15 Mental Health Counselor Near Me Benefits That Everyone
Should Know mental health assessments near me (b.cari.com.my)
The Most Inspirational Sources Of Mobile Auto Diagnostic Services
Near Me Engine Diagnostics