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
XYGENYX, a licensing firm for FDA-registered items.
Feel free to surf to my webpage; ai picture generator reviews
Portable vaporizers utilize batteries as a power source.
My blog post – Bookmarks
Buying Online From Uk To Ireland Tools To Improve Your
Daily Lifethe One Buying Online From Uk To Ireland Trick That
Every Person Must Know buying online from uk to ireland
The vapor and flavor top quality is additionally fantastic.
Also visit my webpage: plant based meat alternatives uk
The 9 Things Your Parents Taught You About Online
Shopping Sites In Uk For Electronics online shopping sites In uk For electronics
How To Save Money On ADHD Private Assesment Private
Adhd Assessment Exeter (http://Www.Google.Com/Url?Q=Http://Demo2-Ecomm.In.Ua/User/Tomatostep6/)
Amazing! This blog looks just like my old one! It’s on a totally different topic
but it has pretty much the same page layout and design.
Great choice of colors!
I most likely modified the entire publication 10 various times.
Check out my blog post: Bookmarks
I’m the owner operator of GOPARTYBUS Perth.
Visit my blog; Bookmarks
USP human development hormone (somatropin).
Also visit my blog – bookmarks
Drug information for patients. Cautions.
can i get generic colchicine prices
Everything information about drugs. Get information here.
10 Things That Your Family Teach You About Window Doctor Near Me window Doctor Near me
Lorem Ipsum has been the industry’s standard dummy text https://kalexkursy.pl/coqrhs of type and scrambled it to make a type specimen book.
NewULife Ranks. Invite to my NewUlife Review.
my blog – a-i generator reviews
where can i buy cheap furosemide
Drug information for patients. Drug Class.
can i get cheap tetracycline for sale
Everything trends of meds. Read now.
Heya i am for the first time here. I came across this board
and I find It truly useful & it helped me out a lot. I hope to give something
back and aid others like you helped me.
Nice blog! Is your theme custom made or did you download it
from somewhere? A design like yours with a few simple tweeks would really make my blog stand out.
Please let me know where you got your theme. Many thanks
You can also decide on your preferred payment process to maintain your funds protected.
Visit my blog; casino79.in
Meds prescribing information. Effects of Drug Abuse.
where can i buy cheap divalproex tablets
Some about medicines. Get now.
Not all CBD gummies are 100% free of THC.
Check out my homepage Bookmarks
What is New U Life SOMADERM Transdermal Gel.
Review my web-site: Bookmarks
Right now it seems like Movable Type is the top blogging platform available right now.
(from what I’ve read) Is that what you are using on your blog?
Five Killer Quora Answers On How To Buy Clothes Online From Uk
how to buy clothes online from Uk; kúRz.de,
I really like it when individuals get together and share opinions.
Great website, keep it up!
Check out my blog post … endoboost
They have products to offer or services to use.
Here is my web blog Bookmarks
USP human growth hormonal agent (somatropin).
my homepage – types of va disability benefits
Pills information. Effects of Drug Abuse.
where to get cialis soft pill
All about medicines. Read information now.
XYGENYX, a licensing business for FDA-registered products.
Feel free to visit my website :: ai headshot
generator canva (Marti)
USP human growth hormonal agent (somatropin).
Also visit my web-site; Bookmarks