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
How To Solve Issues With SEO Tools seo Tool Software
I like the valuable info you provide in your articles.
I’ll bookmark your blog and check again here frequently.
I am quite sure I will learn many new stuff right here!
Good luck for the next!
Автори доповіді вважають, що саме ці документи допоможуть усунути фактична
невідповідність між змістом терміна “інформаційне.
Here is my page https://superkomp.com.ua/yak-vibrati-velosiped/
10 Things People Hate About Best Online Shopping Websites Uk Smead Legal File Storage
What’s up Dear, are you truly visiting this web site on a regular basis, if so after that
you will definitely obtain pleasant know-how.
Heya i’m for the first time here. I found this board and I in finding
It truly useful & it helped me out much. I am hoping to give one
thing again and help others like you aided me.
where can i buy cheap differin without prescription
Drugs information. Drug Class.
how to buy generic thorazine without dr prescription
Some news about medicine. Read information now.
Find Out What Slot Strategies The Celebs Are Using slot strategy [https://xypid.Win/story.php?title=interactive-slots-tips-from-the-top-in-the-business-3]
8 Tips To Enhance Your Double Glazing Luton Game window replacement luton
비아그라 데체 약품
비아그라 대체 약품: ED 치료를 위한 다른 옵션
Many Of The Common Errors People Do With Workers Compensation Legal
workers’ Compensation lawsuit
Guide To Double Glazing Repairs Near Me: The Intermediate Guide To Double Glazing Repairs Near
Me double glazing repairs near me; http://scandwap.xtgem.com,
can i get provera online
5 Laws That Anyone Working In Birmingham Windows Should Be Aware
Of composite doors and windows birmingham
where can i get cheap femara no prescription
This design is wicked! You obviously know how to keep
a reader entertained. Between your wit and your videos,
I was almost moved to start my own blog
(well, almost…HaHa!) Great job. I really enjoyed what you had to say, and more than that, how you presented it.
Too cool!
Ten Upvc Window Repairs That Really Help You Live Better
upvc window repairs
10 Websites To Aid You Develop Your Knowledge About Birth Injury Attorneys Birth Injury Lawsuits
You’ll Never Guess This Online Shopping Uk Amazon’s Tricks online shopping Uk Amazon
What Is The Reason Adding A Key Word To Your Life’s Journey Will Make The Impact Famous Slots
10 Quick Tips To New Slots Online wild slots (http://dott-comm.com/)
Five Killer Quora Answers To Window Replacement
Near Me window Replacement Near Me
7 Secrets About Bunk Beds To Single Beds That Nobody Can Tell You single White bunk Beds
10 Best Facebook Pages That I’ve Ever Seen. Best Slots Popular Slots
– https://Cse.Google.Com.Mt/,
This post presents clear idea for the new viewers of blogging,
that really how to do blogging and site-building.
5 Killer Quora Answers On Shopping Online Uk
Clothes Shopping Online Uk Clothes
What Is The Reason Generalized Anxiety Disorder Is The
Best Choice For You? Symptoms Of Anxiety Disorders
Good day I am so glad I found your web site, I really found
you by mistake, while I was searching on Aol for something else, Nonetheless I am
here now and would just like to say many thanks for a marvelous post and a all
round entertaining blog (I also love the theme/design),
I don’t have time to look over it all at the moment but I have book-marked it and also
added your RSS feeds, so when I have time I will be back to read a great deal
more, Please do keep up the excellent job.
10 Strategies To Build Your Assessing Mental Health Empire dcfs Mental health Assessment