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
https://images.google.com.gt/url?q=https://www.openlearning.com/u/maciasdriscoll-sefhh2/blog/CrystalClearTheUltimateGuideToAutoGlassReplacement
The Most Sour Advice We’ve Ever Received On Folding Treadmills
UK zackfoxworth.top
Hi there would you mind sharing which blog platform you’re working
with? I’m looking to start my own blog in the near future but I’m having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems different then most blogs
and I’m looking for something completely unique.
P.S Apologies for being off-topic but I had to ask!
20 Things You Need To Know About Coffeee Machine Prices Of Coffee Machines
(Escortexxx.Ca)
I am sure this post has touched all the internet people, its really really
nice piece of writing on building up new weblog.
Introduction In recent years, the prevalence of diabetes and other blood sugar-related issues has been on the rise, leading to increased interest in finding effective solutions to manage these conditions.
my site:: https://glucopremium.net
Hello! get generic finasteride without a prescription beneficial web site.
All The Details Of Upvc Door Panels Dos And Don’ts Upvc door Lock
The History Of Coffee Maker Bean To Cup coffee To cup Machine
Why You Should Focus On Improving Repair Upvc Window upvc window repairs
Lorem Ipsum has been the industry’s standard dummy text https://strzelamyfoty.pl/uacfo of type and scrambled it to make a type specimen book.
15 Bean To Cup Coffee Maker Benefits Everybody Should Know bean To cup espressomaschine –
http://www.radioveseliafolclor.Com –
Najbolji savet za kupovinu auta cesto nadem medu komentarima.
Citroen nudi odlicnu garanciju na svoja vozila.
Voziti polovan auto je kao hodanje po tankom ledu.
بحثت مطولا عن اسرع الطرق لتعلم اللغة الانجليزية و بتوفيق من الله تواصلت عن طريق بعض الأصحاب مع الشاب كاموستا في الفلبين حيث انه قام بمساعدتي بالحصول على
مقعد لدراسة اللغة الانكليزية في معهد محترم جدا
و قام بتأمين كل متطلباتي من المسكن الجيد و المواصلات و مساعدتي بالوصول لمتطلباتي من الاطعمة الحلال كانت حصص الدراسة في المعهد سريعة يوميا و قام بالتواصل مع اساتذة لإجراء حصص
خاصة , في مدة قصيرة بدأت التحدث
بطلاقة و ثقة كانت تجربة رائعة انصح بها كل من يريد دراسة اللغة الانجليزية
للتواصل مع الشاب كاموستا على الرابط
kumoosta.com
A片
I’m amazed, I must say. Seldom do I come across a blog
that’s both educative and entertaining, and let me tell you, you’ve hit the nail on the head.
The issue is something too few people are speaking intelligently about.
I’m very happy I found this in my hunt for something regarding this.
Howdy would you mind stating which blog platform
you’re working with? I’m looking to start my own blog soon but I’m having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems different then most blogs and
I’m looking for something unique. P.S Sorry
for getting off-topic but I had to ask!
It’s a pity yⲟu don’t have а donate button! I’d
with᧐ut a doubt donate tоo this excellent blog! Ι suppose fⲟr now
i’ll settle for book-marking and adding your RSS feed
tߋo my Google account. I lоok forward to fresh updates and ѡill sare ths site ᴡith my Facebook gгoup.
Talk soon!
Heere іs my blog post qq cuan slot
how to buy cheap lisinopril without dr prescription
This is a topic which is close to my heart… Cheers! Where are your contact details though?
cheap abilify
I’m ѵery pleased tto discover tһis site. I want tо to than you for yoսr timе due
tօ this fantaqstic read!! I dеfinitely savored еverү bіt
of it annd I have you bokmarked t᧐ check οut new informɑtion on yoսr web site.
Here iss myy page Slot Terbaru
20 Insightful Quotes On Workers Compensation Litigation workers’ compensation attorneys
Woah! I’m really loving the template/theme of this blog.
It’s simple, yet effective. A lot of times it’s very hard
to get that “perfect balance” between usability and visual appeal.
I must say you’ve done a fantastic job with this.
Additionally, the blog loads very quick for me on Safari.
Excellent Blog!
I do not even know how I ended up here, but I thought this post was great.
I do not know who you are but definitely you are going to a famous blogger if you aren’t already 😉 Cheers!
The One Boot Mobility Scooters Mistake Every
Beginner Makes arlennizo.top
Medication information. Short-Term Effects.
where buy cheap irbesartan without dr prescription
Actual what you want to know about medicament. Read information here.
What Erb’s Palsy Case Experts Want You To Know Erb’s palsy lawsuits
https://ddnews.co.kr/category/welfare/silver/
This paragraph will assist the internet people for building up
new web site or even a blog from start to end.
Asbestos Attorneys Tips That Can Change Your Life Cassy Lawn