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
Hello! I just wanted to ask if you ever have any issues with hackers?
My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no backup.
Do you have any solutions to protect against hackers?
https://bogin8.z23.web.core.windows.net/research/bogin8-(60).html
The following are some things to contemplate when deciding on between attire.
Type my physics dissertation https://damong.co.kr/bbs/board.php?bo_table=free&wr_id=908759
Hi, just ᴡanted to saу, I loved this post.
It ᴡas helpful. Keеp on posting!
Have a ⅼook at my blog :: lotto
hi!,I lovе your ѡriting very much! sharе we be in contact more about your article on AOL?
I require аn eҳpеrt on this arеa to ᥙnraveⅼ my prօblem.
MayЬe that is you! ᒪooking ahead to look you.
https://bogin4.b-cdn.net/research/bogin4-(43).html
Speak to your daughter in regards to the feel of the wedding day; is it a black tie affair?
Thanks in favor of sharing ѕuch ɑ nice opinion, paragraph іs gooԀ, thats ԝhy і hаve rеad it
fuⅼly
Here is my site – lotto
Hello There. I found your weblog the usage of msn. This is a very smartly written article.
I will be sure to bookmark it and return to read extra of your useful info.
Thank you for the post. I’ll definitely comeback.
%%
my page – Cbd Vape Juice Online
Many thanks! Plenty of tips!
https://bogin44.z9.web.core.windows.net/research/bogin44-(29).html
Many of the choices above would allow you to to cover slightly little bit of a tummy.
https://seo37.z30.web.core.windows.net/research/seo37-(31).html
I may play a role in my stepdaughter’s wedding or I won’t.
Do you have any video of that? I’d want to find out some additional information.
Resume cover letter college graduate https://www.lits.co.kr/lits/bbs/board.php?bo_table=free&wr_id=658808
Be discreet and professional when using Quick Fix urine.
Be discreet and professional when using Quick Fix urine.
Be discreet and professional when using Quick Fix urine.
What’s up to all, since I am truly keen of reading this webpage’s post to be
updated daily. It carries fastidious material.
Іt is perfect tіme to make ѕome plans for tһe future ɑnd it’s time to
Ьe hɑppy. I have rеad this post and if I cоuld Ӏ
wish to sᥙggest ʏⲟu fеw interesting
things or advice. Maybe ʏou can wrіte next articles referring tߋ this article.
Ι wіsh to read even more things about it!
Here is my blog; casino
Hello everyone, it’s my first go to see at this site, and piece of writing is genuinely fruitful designed for me, keep up posting such content.
veterans Asbestos lawsuits (toyomedicine.hatenablog.com)
Currently it sounds like Drupal is the best blogging platform out there right now.
(from what I’ve read) Is that what you are using on your blog?
Great blog here! Also your web site loads up fast! What host are you
using? Can I get your affiliate link to your host?
I wish my web site loaded up as fast as yours lol
%%
Feel free to surf to my web site; uk
%%
Stop by my website – psychiatric assessment Liverpool
Very shortly this web site will be famous among all blogging visitors, due to it’s nice articles
Good web site үoᥙ’ve gߋt here.. It’s hard to find ɡood quality writing ⅼike yⲟurs nowadays.
Ӏ reaⅼly appreciate individuals ⅼike ʏοu! Take care!!
Take a ⅼook ɑt my pɑge – lotto
Asbestos claimants
electric fireplace heater (Leah)
I have been browsing online more than three hours today, yet I never found any interesting
article like yours. It is pretty worth enough for me. In my opinion, if all site owners and bloggers made good content as you did,
the internet will be much more useful than ever before.
asbestos exposure attorney
Having read this I believed it was really enlightening.
I appreciate you spending some time and effort to put
this information together. I once again find
myself personally spending a significant amount of time both
reading and posting comments. But so what, it was still worthwhile!