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
Thanks for sharing your info. I truly appreciate your efforts and I
will be waiting for your next post thank you once again.
Hi! This is my first comment here so I just wanted to give a
quick shout out and say I truly enjoy reading through your blog posts.
Can you recommend any other blogs/websites/forums that deal with the same topics?
Thanks for your time!
If you wish for to get much from this piece of writing then you have to apply such techniques to your won web site.
10 Things That Your Family Taught You About Managing ADHD Without Medication Managing adhd without medication
10 Things That Your Family Taught You About Compact Travel
Scooters Compact travel scooters
claritin d pharmacy: abc pharmacy store locator – watch tour de pharmacy online free
Hey there! I just wanted to ask if you ever have any problems with
hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no data backup.
Do you have any methods to protect against hackers?
Here is my website: lungs clear pro
viagra malaysia pharmacy: Lamictal – mexico viagra pharmacy
Guide To Treadmill For Home Use: The Intermediate Guide For Treadmill For Home Use treadmill for home (http://www.cheaperseeker.com)
Oh my goodness! Incredible article dude! Many thanks, However I am
experiencing issues with your RSS. I don’t
know the reason why I cannot join it. Is there anybody having identical RSS issues?
Anyone who knows the answer will you kindly respond? Thanx!!
Normally I do not learn post on blogs, but I wish to say that this write-up very compelled me to take
a look at and do so! Your writing style has been surprised me.
Thank you, very nice article.
духовные ретриты что это https://ретриты.рф
Hey! This post couldn’t be written any better!
Reading through this post reminds me of
my old room mate! He always kept chatting about this. I will
forward this article to him. Fairly certain he will have a
good read. Thank you for sharing!
Remarkable things here. I’m very satisfied to peer your post.
Thanks a lot and I am taking a look forward to touch you.
Will you please drop me a mail?
Don’t Stop! 15 Things About Depression Therapy We’re Tired
Of Hearing How Long Does Depression Treatment Last
Comfax.com is a mobile app that lets you send and receive faxes directly from your smartphone.
Say goodbye to bulky fax machines and paper hassles—now it’s all in your pocket.
The app works with various file formats and ensures strong security with data encryption.
Comfax.com allows you to quickly send important documents with
just a few taps and track their delivery in real time.
Convenient, safe, and always available.
Download Comfax.com now and make your life easier.
7 Tips To Make The Greatest Use Of Your Car Key Mobile Locksmith locksmith car keys replacement (Rosaura)
atomoxetine online pharmacy: pharmacy warfarin counselling – rx solution pharmacy
Do you mind if I quote a few of your posts as long as I provide credit
and sources back to your weblog? My website is in the very same area
of interest as yours and my users would genuinely benefit from
some of the information you provide here. Please let me know if this okay
with you. Cheers!
20 Fun Informational Facts About Adult Toy Store interactive Adult toy
provigil online us pharmacy: giant food store phoenixville pharmacy – ibuprofen singapore pharmacy
giant food store pharmacy: Oxytrol – indian pharmacy viagra
What’s up to every body, it’s my first go to see of this
weblog; this webpage carries remarkable and really excellent stuff
in favor of visitors.
Диплом вуза купить официально с упрощенным обучением в Москве
wedgewood pharmacy prednisolone: pharmacy prices estradiol – indian pharmacy finasteride
Thank you for the auspicious writeup. It in fact
was once a leisure account it. Look complicated to far brought agreeable from you!
By the way, how can we communicate?
Heya i am for the first time here. I came across
this board and I find It really useful & it helped me out a lot.
I hope to give something back and aid others like you helped me.
Medicament information for patients. Long-Term Effects.
where buy generic zithromax without rx
Actual news about medication. Get now.
You have made some really good points there. I looked on the net for additional information about the issue and found most
individuals will go along with your views on this
site.
Hi! I could have sworn I’ve visited this web site before but after browsing
through many of the articles I realized it’s new to me. Regardless, I’m definitely
happy I found it and I’ll be book-marking it and checking back regularly!