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
My partner and I stumbled over here from a different website and thought I may as
well check things out. I like what I see so now i’m following you.
Look forward to looking over your web page again.
Hello there! Would you mind if I share your blog with my zynga group?
There’s a lot of people that I think would really appreciate your content.
Please let me know. Many thanks
Hmm it appears like your blog ate my first comment (it was extremely long) so I guess I’ll
just sum it up what I had written and say, I’m thoroughly enjoying your blog.
I too am an aspiring blog writer but I’m still new to everything.
Do you have any tips and hints for newbie blog writers?
I’d genuinely appreciate it.
Lorem Ipsum has been the industry’s standard dummy text https://befitjusteat.pl/kawgkh of type and scrambled it to make a type specimen book.
15 Incredible Stats About Slot Sound Effects fair slots
Kudos, Ample posts.
The Top Car Accident Lawyers Gurus Are Doing Three Things Car Accidents
why does colchicine cause diarrhea
Undeniably believe that which you said. Your favorite reason appeared to be
on the internet the simplest thing to be aware of. I say to
you, I definitely get annoyed while people think about worries that they plainly do not know about.
You managed to hit the nail upon the top and also defined out
the whole thing without having side-effects , people can take a signal.
Will likely be back to get more. Thanks
Also visit my web blog hookup for sex near me
Personal Injury Lawyers Tools To Streamline Your Everyday Lifethe Only Personal Injury Lawyers
Trick That Should Be Used By Everyone Know personal injury lawyers
15 Things You’re Not Sure Of About Wordai Versus Article Rewriter What is Word ai
5 Laws That Will Help The Motor Vehicle Claim Industry motor vehicle accident law firms
I’m no longer sure the place you’re getting your information, however good topic.
I needs to spend a while finding out more or figuring out more.
Thank you for wonderful info I was on the lookout for this info
for my mission.
Ganhe muito no melhor cassino Blaze
20 Tips To Help You Be More Efficient With Birth Injury Compensation Birth Injury Lawsuits
where can i buy cheap promethazine pill
10 Wrong Answers To Common Repairs To Upvc Windows Questions Do You Know Which
Ones? upvc Window repair
Your Family Will Thank You For Having This SEO Consulting Company Top content marketing Companies
Hey there! I know this is kind of off-topic but I needed to ask.
Does managing a well-established website like yours require a massive
amount work? I’m brand new to writing a blog however I do
write in my diary on a daily basis. I’d like to start a blog so
I can share my personal experience and views online.
Please let me know if you have any kind of recommendations or tips for brand new aspiring bloggers.
Thankyou!
What Is Slot Gaming And Why Is Everyone Dissing It? casino slots guide
Seven Explanations On Why Birth Defect Settlement Is Important birth defects (https://Dasibogi.live/)
Amazing things here. I’m very happy to look your article.
Thanks so much and I’m taking a look forward to touch
you. Will you kindly drop me a mail?
My spouse and I absolutely love your blog and find nearly all of your post’s
to be what precisely I’m looking for. Would you offer guest
writers to write content for you personally?
I wouldn’t mind writing a post or elaborating
on a number of the subjects you write about here.
Again, awesome blog!
prednisone generic and trade name
Utilisez des stratégies de segmentation et de targeting créatives pour maximiser vos performances.
Thibaut Nicolas Marc Courtois https://thhibaut-courtois.prostoprosport-fr.com Footballeur belge, gardien de but du club espagnol du Real Madrid . Lors de la saison 2010/11, il a ete reconnu comme le meilleur gardien de la Pro League belge, ainsi que comme joueur de l’annee pour Genk. Triple vainqueur du Trophee Ricardo Zamora
The Most Negative Advice We’ve Ever Received On Slot Strategies
slot payouts (vdruzhkovke.net.Byjeanne.Com)
One Auto Accident Attorney Success Story You’ll Never Be Able To
Auto Accident Lawsuits