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
See What Seo Backlink Building Software Tricks The Celebs Are Making
Use Of seo Backlink building software
legal to buy prescription drugs without prescription low cost pharmacy canada ed drugs
can i order super p-force no prescription
My brother suggested I would possibly like this web site.
He was totally right. This publish actually made my day. You cann’t consider simply how so much time I
had spent for this information! Thanks! jili slot
10 Erroneous Answers To Common Cheap Sofas For Sale Questions: Do
You Know The Right Answers? Cheap Couches For Sale Under $200
The vitality efficient dwelling enchancment credit.
you are in point of fact a just right webmaster.
The website loading speed is amazing. It kind of feels that you’re doing any distinctive
trick. Moreover, The contents are masterpiece. you have done a great task in this matter!
online pharmacy india Order medicine from India to USA best online pharmacy india
OHANA99เว็บสล็อตมากความสามารถที่ให้มากกว่าความสนุกสนาน
buy drug online https://mexicanpharm24.cheap/# buying from online mexican pharmacy
5 Killer Quora Answers On Software For SEO Software for Seo
Hello there, I believe your blog could be having browser compatibility
issues. Whenever I take a look at your web site in Safari, it looks fine but when opening in I.E., it’s got some overlapping issues.
I just wanted to provide you with a quick heads up!
Other than that, wonderful site!
Greetings! Very useful advice in this particular post! It is the little
changes which will make the most important changes. Thanks a
lot for sharing!
There is certainly a lot to find out about this topic. I like all the points you made.
10 Amazing Graphics About Misty Windows Repair misty windows repair near me (Debra)
This page truly has all the information I wanted about this subject and didn’t know who to ask.
xxx
1win
Hi! I just wanted to ask if you ever have any problems with hackers?
My last blog (wordpress) was hacked and I ended up losing months
of hard work due to no back up. Do you have any solutions to protect against hackers?
Comprehensive medicine resource. Get info now.
order prednisone online
Medication leaflet provided. Pill impacts described.
http://pharm24.pro/# best treatment for ed
how to fix ed
The Ultimate Glossary On Terms About Pragmatic Free 프라그마틱 슬롯체험
(https://lovebookmark.date/story.php?title=20-inspiring-quotes-about-free-pragmatic)
https://mexicanpharm24.cheap/# purple pharmacy mexico price list
best medicine for ed
The 10 Most Scariest Things About Glass Repair Birmingham Window Companies In Birmingham
mexican rx online mexican drugs buying from online mexican pharmacy
Guide To Tots And Cots: The Intermediate Guide In Tots And Cots tots and cots [Alexis]
Hey there this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to
manually code with HTML. I’m starting a blog soon but have no coding skills so I wanted to get guidance from
someone with experience. Any help would be enormously appreciated!
Hello to every body, it’s my first pay a visit of this webpage; this
weblog carries awesome and genuinely excellent material in favor of visitors.
best treatment for ed http://pharm24.pro/# ed remedies
WOW just what I was looking for. Came here by searching for
que es 1win
That is a really good tip particularly to
those fresh to the blogosphere. Brief but very accurate information… Thanks for sharing
this one. A must read article!