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
You could certainly see your expertise within the work you write.
The world hopes for even more passionate writers like you who are not afraid to mention how they believe.
At all times go after your heart.
What’s The Current Job Market For Veterans Disability Compensation Professionals?
Veterans disability
where can i buy generic cozaar without prescription
See What Best Online Shopping Uk Clothes Tricks The Celebs Are Using best online Shopping uk clothes [http://www.ures.co.kr]
Double Glazing Door Repairs Near Me Tools To Ease Your Daily Life Double Glazing Door Repairs Near Me Trick That
Everyone Should Be Able To glaze
Hello to every one, because I am actually keen of reading this website’s post to be updated daily.
It contains good information.
The 10 Most Scariest Things About Double Glazing Company Near Me
double glazing Company Near me
can you buy generic lisinopril for sale
The level of realism is remarkable,with lifelike skin texture and finely detailed facial features.ラブドール 中古
Online Casino Slots: What’s The Only Thing Nobody Is Talking About evoplay slots quests – https://forum.nofap.com/,
10 Wrong Answers To Common White Cabin Beds Questions: Do
You Know The Right Ones? cabin Beds with slides
Medicament information. Short-Term Effects.
nolvadex pill
Everything trends of medicament. Read information now.
The Most Underrated Companies To Watch In The Upvc Door Panel Industry
panels for Upvc Doors
Your style is unique in comparison to other people I’ve read
stuff from. Many thanks for posting when you’ve got the opportunity, Guess I will
just bookmark this web site.
10 Facts About Upvc Window Repairs That Can Instantly Put You In Good Mood 257634.xyz
where can i get betnovate without a prescription
20 Resources To Help You Become Better At Boat Accident
Compensation boat accident Attorneys
5 Double Glazing Repair Near Me Lessons From The Pros jerealas.top
Medicament information leaflet. Brand names.
where buy generic nortriptyline pill
Best information about meds. Read here.
раскрутка и продвижение сайтов в москве http://www.prodvizhenie-sajtov-v-moskve115.ru .
продвижение сайтов в москве цены http://www.prodvizhenie-sajtov-v-moskve115.ru/ .
продвижение сайтов в москве в яндекс и гугл prodvizhenie-sajtov-v-moskve115.ru .
I love your blog.. very nice colors & theme.
Did you create this website yourself or did you hire
someone to do it for you? Plz respond as I’m looking to construct my own blog and would like to know where u got this from.
thanks a lot
seo продвижение сайтов в москве агентство prodvizhenie-sajtov-v-moskve115.ru .
15 Surprising Stats About How To Buy Clothes Online
From Uk Luxury Travel Luggage
9 . What Your Parents Taught You About Double Glazed Window
Suppliers Near Me double Glazed window suppliers near Me
The Repair Upvc Window Case Study You’ll Never Forget sash
10 Things You Learned In Preschool That Will Help You With Railroad Injuries Litigation railroad
injuries lawyers (justbookmark.win)
Pills information. Drug Class.
where can i buy generic zithromax for sale
Some news about medication. Read information now.
10 Untrue Answers To Common Truck Accident Questions: Do You Know The Right Ones?
Truck Accident Lawsuits