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 know this is kinda off topic but I was wondering which blog
platform are you using for this site? I’m getting sick and tired of WordPress because I’ve
had problems with hackers and I’m looking at options
for another platform. I would be great if you could point me in the direction of
a good platform.
My webpage – Daycares By Category
Drug prescribing information. Short-Term Effects.
get generic zithromax without rx
All information about meds. Read information here.
15 Retro Fridge Freezer Black Benefits Everybody Should Be Able To white Side by side fridge Freezer
ВодкаКазино
I must thank you for the efforts you have put in penning
this website. I really hope to check out the same high-grade blog posts by you later on as well.
In fact, your creative writing abilities has
motivated me to get my very own website now 😉
Here is my blog – ngentot
pharmacy on line: fluconazole pharmacy – cheapest order pharmacy viagra
Hi this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding know-how so I wanted to get
advice from someone with experience. Any help would be
greatly appreciated!
I am sure this paragraph has touched all the internet users, its really really good post on building up new weblog.
Если вы искали где отремонтировать сломаную технику, обратите внимание – ремонт техники в челябинске
It’s genuinely very difficult in this busy life to listen news on Television, thus I
just use world wide web for that reason, and
get the most up-to-date information.
Thank you for the good writeup. It in fact was a amusement account it.
Look advanced to more added agreeable from you! By the way, how can we communicate?
Are You Getting The Most Out You Reprogramming Car
Key? reprogramme car key (Shelly)
There’s definately a lot to find out about this topic.
I like all the points you have made.
Если вы искали где отремонтировать сломаную технику, обратите внимание – ремонт бытовой техники в челябинске
Thanks for your personal marvelous posting! I truly enjoyed reading it,
you happen to be a great author.I will remember to bookmark your blog and will come back in the foreseeable future.
I want to encourage you to definitely continue your great job, have a nice
morning!
Wonderful article! We are linking to this great content
on our site. Keep up the great writing.
It’s hard to come by knowledgeable people on this subject, however,
you seem like you know what you’re talking about!
Thanks
Asking questions are in fact pleasant thing if
you are not understanding anything entirely, however this
post presents nice understanding yet.
Если вы искали где отремонтировать сломаную технику, обратите внимание – профи ремонт
10 Things That Your Family Teach You About Program Car Key car
online pharmacy cellcept: propecia at boots pharmacy – online pharmacy percocet no prescription
Why To Consider An Unsecured Business Loan 다바오 지진
winn-dixie pharmacy: hong kong pharmacy ambien – cialis pharmacy australia
pharmacy store window: reddit best online pharmacy – allergy
9 . What Your Parents Taught You About L Shape Leather Sofa leather sofa
It’s amazing designed for me to have a website, which is valuable for my
know-how. thanks admin
I was excited to uncover this web site. I wanted to thank
you for your time for this fantastic read!!
I definitely loved every little bit of it and i also have you saved to fav
to see new information on your web site.
viagra no prescription online pharmacy: escitalopram generic pharmacy – depakote online pharmacy
Spot on with this write-up, I seriously feel this website needs much more attention. I’ll probably be back again to read through more, thanks for
the info!