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
5 Killer Quora Answers To Play Slots Play Slots
Buzzwords De-Buzzed: 10 Different Ways To Deliver Private Mental Health Assessment Near Me Mental health Assesment
11 Creative Methods To Write About Jackpot Slots Random Jackpot slots
where can i get generic accutane
where to buy elimite without prescription
I’m pretty pleased to discover this website. I want
to to thank you for ones time just for this wonderful read!!
I definitely loved every little bit of it and i also have you
book marked to look at new stuff in your site.
You’ll Be Unable To Guess GSA SER Coupon’s Tricks gsa ser
The Top Reasons Why People Succeed In The SEO Software UK Industry seo traffic software
This is my first time pay a quick visit at here and i am actually impressed to read everthing at single place.
lisinopril 10 mg tablet cost
I used to be suggested this web site by way of my cousin. I am no longer
certain whether or not this publish is written via him as nobody else understand such
distinctive about my trouble. You are incredible!
Thank you!
my web page – fitspresso reviews
What’s up to all, how is everything, I think every one is getting more from this site,
and your views are fastidious in support of new people.
Hi to every one, the contents existing at this site are truly remarkable for people experience,
well, keep up the nice work fellows.
5 Tools That Everyone Is In The Slot Gaming Experience Industry Should Be Using best payouts slots
What’s The Job Market For Car Accident Attorney Professionals Like?
Car accident
Stop by my homepage; safest poker sites
buy generic betnovate no prescription
15 Things You’re Not Sure Of About New Slots Online reputable Slots
Great post.
Feel free to visit my site – craiglistforsex
can i purchase duphalac prices
How Much Do SEO Companies Near Me Experts Earn? near me seo strategy
Приветствую. Подскажите, где почитатьполезные блоги о недвижимости? Пока нашел https://universal37.ru
That is a great tip particularly to those fresh to the
blogosphere. Simple but very precise info… Appreciate your sharing this one.
A must read article!
10 Motor Vehicle Settlement-Friendly Habits To Be Healthy motor vehicle accident lawyer
I don’t know wether it’ѕ jᥙst me or if everyolne еlse encountering рroblems wth y᧐ur site.
Ιt ѕeems ⅼike sоme of the written ttext іn yοur
posts ɑre running off the screen. Can somеbody else
please comment and let me knoԝ if tbis is happening tօ them tоo?
Thhis miցht be a issue ѡith mmy internet browser Ьecause I’ve hhad thіs happpen pгeviously.
Apprеciate it
Also visit my homepagе Arena333 Slot Login Link Alternatif
can i purchase cheap cipro without rx
Birth Injury Lawyers Tools To Streamline Your Daily Life Birth Injury Lawyers Technique Every Person Needs To Know Birth Injury
You’ll Never Be Able To Figure Out This Ecommerce
SEO Agency’s Tricks seo
This Is The History Of Treadmill Incline Foldable In 10 Milestones zackfoxworth