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
avodart dosage for hair loss
I love your blog.. very nice colors & theme. Did you make
this website yourself or did you hire someone to do it for you?
Plz reply as I’m looking to create my own blog and would like to find out where u got
this from. thank you
wonderful issues altogether, you just received a new reader.
What may you recommend about your publish that you simply made some days in the past?
Any certain?
Hello, i think that i saw you visited my blog so i came to “return the favor”.I’m trying to find things to enhance my
web site!I suppose its ok to use a few of your ideas!!
Thanks a lot, Lots of posts!
Here is my web site … https://Highwave.kr/bbs/board.php?bo_table=faq&wr_id=2726546
of course like your web-site but you need to
test the spelling on quite a few of your posts.
A number of them are rife with spelling issues and I find it very
troublesome to inform the truth nevertheless I will definitely come back
again.
You’ll Never Guess This Anxiety Treatment Natural’s Tricks anxiety treatment natural
Five Killer Quora Answers On Replacement Double Glazing Units Near
Me replacement double Glazing units near me
You actually make it seem so easy with your presentation but I find this topic to be actually something that I think I would never understand.
It seems too complex and extremely broad for me. I’m looking forward
for your next post, I will try to get the hang of it!
The 10 Scariest Things About Top Single Strollers Top Single strollers
where to get cheap caduet
When I initially left a comment I seem to have clicked on the
-Notify me when new comments are added- checkbox and now each time a comment is added I receive
4 emails with the same comment. Perhaps there is a way you
can remove me from that service? Appreciate it!
13 Things You Should Know About Slot Sites
That You Might Not Know Real Money Slots (Dencker-Burnham.Mdwrite.Net)
The History Of Auto Accident Legal auto accident law firms – https://www.2chan.net/bin/jump.php?https://vimeo.com/707177909,
Hеllo there I am so thrilled I found your website, I гeally found you by mistakе,
while I was Ƅrօwsing on Yahoo for somethіng else, Nonetheless
I am here now and would just like to say many thanks for a
marvelous poѕt and a all round exciting blog (I also love the theme/design), I don’t have
time to read it all at the minute but I have
bookmarked it and also added your RSS feeds, so when I have time
Ι will be back to гead more, Please do keeρ up the superb work.
Also visit my homepage … Dedicatedproxies.Net
A Look At The Future What Is The Glass Door Repair Cambridge Industry Look Like In 10 Years?
Timber Double Glazed Windows Cambridge (http://Cse.Google.Co.Kr/Url?Sa=T&Url=Http://Www.Diggerslist.Com/6652688Cdadba/About)
Pills information sheet. Short-Term Effects.
buy generic inderal without prescription
Best information about medicines. Read now.
The 10 Most Scariest Things About High Wycombe Windows And Doors Aluminium Doors
High Wycombe; http://Www.Bevnp.Com,
Why Do So Many People Want To Know About Winning Slots? Trusted Slots
10 Things People Get Wrong About The Word “Greenpower Scooter.” green Power
scooter reviews (http://Dia.Meterxoql@brewcitymusic.com/guestbook/index.php/RK=0/RS=ycuXl8jUDcV830UeENmS771R.Jw-)
Komentari su kao retrovizor – omogucavaju pogled unazad na razlicite stavove.
Chevrolet Vega je bio inovativni model iz 70-ih.
Auto mi je toliko star da mu treba vlastiti istorijski spomenik.
What’s The Job Market For Online Shopping Uk For Clothes Professionals Like?
online shopping uk for clothes
Hello there! Would you mind if I share your blog with my facebook group?
There’s a lot of folks that I think would really enjoy your content.
Please let me know. Many thanks
New customers also have the alternative for this weekend with a welcome present
of $10 in Bonus Bets with any $20 bet on on NFL Sunday games November 5.
My web page :: 메이저사이트
Благодарю ! Заходите и еще на собственный сервис 😉 Застекления балкона под ключ
Thanks for your personal marvelous posting! I quite enjoyed reading it,
you happen to be a great author.I will be sure to bookmark your blog
and will eventually come back at some point. I want to encourage you to ultimately continue your great writing, have a nice weekend!
Does your site have a contact page? I’m having a tough time locating
it but, I’d like to send you an e-mail. I’ve got some
creative ideas for your blog you might be interested in hearing.
Either way, great website and I look forward to seeing
it expand over time.
10 Healthy ADHD Adult Treatment Habits Symptoms Of Untreated Adhd In Adults
On-line media and marketing.
Also visit my web-site cosori air fryer – Alecia –
3 Ways That The Window Repair Near Can Affect Your
Life Upvc Window repairs