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
What The 10 Most Worst Sofas L Shape Failures Of All Time Could Have Been Prevented
sleeper couch L Shape
In this instance, the life insurance policy company’s cash.
Feel free to surf to my website :: Bookmarks
The IQ is the most effective dry natural herb plant based vape uk I have actually attempted.
Thanks for some other magnificent article. The place else may anyone get
that kind of information in such a perfect means of writing?
I’ve a presentation subsequent week, and I am at the search for such information.
My blog post; are seo services worth it
NewULife Ranks. Invite to my NewUlife Review.
Here is my web site – ai headshot generator free canva
система для конференц зала https://oborudovanie-konferenc-zalov11.ru/ .
Lorem Ipsum has been the industry’s standard dummy text https://miglierinacomunitaospitale.it/sycnezb of type and scrambled it to make a type specimen book.
I probably edited the entire publication 10 different times.
Here is my site book editing services in india
Discovering a language learning apps reddit
does not need to be costly.
Asbestos Compensation victims seek compensation from companies responsible for asbestos exposure.
This is done through personal injury claims and wrongful deaths.
A licensed attorney will guide the families of victims through the legal process.
Organic Lion’s Mane Mushroom Essence for $39.
My web site :: Bookmarks
Technology Is Making Emergency Car Locksmith Better Or Worse?
http://www.elsycrays.top
I got this web page from my friend who told me regarding
this website and now this time I am visiting this website and reading very informative articles or reviews here.
This Is How ADHD In Adult Women Symptoms Will Look In 10
Years Adhd Inattentive Type Women
Medicament information. Effects of Drug Abuse.
where can i get cheap anastrozole tablets
Best about drug. Get now.
Guide To Online Clothes Shopping Near Me: The Intermediate Guide The Steps To Online Clothes
Shopping Near Me online clothes shopping Near Me
Five Killer Quora Answers On Double Glazed Window Repairs Near Me double glazed window repairs near me
USP human growth hormonal agent (somatropin).
My page: ai picture generator free reddit
What is New U Life SOMADERM Transdermal Gel.
Here is my page – how long does it take to learn motion graphics
Hey! This is my first visit to your blog! We are a collection of volunteers and starting a new initiative in a community in the same niche.
Your blog provided us useful information to work on. You have done a wonderful job!
Pay Attention: Watch Out For How Mercedes Spare Key Is Taking Over And What Can We Do
About It Mercedes keys Replacement
Free time is perfect for unwinding and enjoying what you love most. You can enjoy this time reading a good book. For those seeking something different, Casino Intense offers entertaining activities like slots, table games, and live casino. With daily bonuses and an easy-to-use platform, it’s a superb option to try new forms of entertainment. Let us know how you spend your weekends and try new activities to make your leisure time even more enjoyable.
Pick a brand that uses natural ingredients.
Feel free to surf to my blog – which is better for anxiety cbd oil or gummies
how can i get cheap plavix without dr prescription
Thanks for a marvelous posting! I definitely enjoyed reading it, you are a great
author. I will ensure that I bookmark your blog and will come back from now on. I want to encourage you to ultimately continue your great job, have a nice morning!
Good article. I am goіng throuɡh many of tһese iwsues as
weⅼl..
Мy page :: slot gacor
Tier 2 Links Seo Tools To Streamline Your Everyday
Lifethe Only Tier 2 Links Seo Trick That Everybody Should Learn tier 2 links seo
I’m the proprietor driver of GOPARTYBUS Party Bus perth Wa.
Genuine HGH Advantages (NewULife Hgh Gel Not Checked Yet).
Here is my homepage ai picture generator reviews
See What Double Glazing Repair Near Me Tricks The Celebs Are Utilizing Double Glazing Repair Near Me