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
How Demo Akun Has Changed My Life The Better Oscar Reys
Medication information leaflet. Drug Class.
where can i buy cheap zofran price
Actual trends of medicament. Read now.
propecia medicine in india
This History Behind SEO Consultant In London Can Haunt You Forever!
Cheap
A Look At The Good And Bad About Double Glazing Installers Near
Me double glazed wooden windows
The Best Advice You Can Ever Receive On Boot Scooters
arlennizo.top
Medicament information for patients. Effects of Drug Abuse.
can you get cheap ramipril without a prescription
Best about medicines. Get now.
Here is my site … top 10 poker websites (Archer)
You Can Become A Credit History Personal Loan For $5,000 중기청 대출
Hello, of course this article is really nice and I have learned lot of things from it regarding blogging.
thanks.
Hi, I want to subscribe for this website to take most recent updates, therefore where
can i do it please help.
How Much More Your Balance Transfers Credit Card 신혼부부 대출
When someone writes an article he/she retains the thought of a user in his/her brain that how a user
can know it. Thus that’s why this piece of writing is
outstdanding. Thanks!
hacklink
5 Attorney For Asbestos Projects For Any Budget Alfred
My brother suggested I might like this web site.
He was entirely right. This post truly made my day.
You cann’t imagine simply how much time I had spent for this info!
Thanks!
What’s The Current Job Market For Upvc Repairs Near Me Professionals?
Upvc repairs near me (talkstarsi.com)
Great article.
Also visit my site Does PhenQ Work
9 Things Your Parents Teach You About ADHD Uk Medication Adhd uk medication
Medicines information for patients. Short-Term Effects.
can i get generic zocor prices
Everything news about medicines. Get now.
lesbian porn
Drugs information. Short-Term Effects.
how to get cheap zithromax without dr prescription
Actual trends of medicines. Read here.
Pills information leaflet. Cautions.
where to get generic indomethacin no prescription
Actual what you want to know about medication. Get information here.
Because the admin of this web site is working,
no hesitation very rapidly it will be renowned, due to its feature contents.
Have you ever thought about writing an ebook or guest authoring on other websites?
I have a blog centered on the same subjects you discuss
and would really like to have you share some stories/information. I know my
viewers would enjoy your work. If you’re even remotely interested,
feel free to shoot me an e-mail.
Nine Things That Your Parent Teach You About Double
Glazing Repair Near Me near me
Hey there! Would you mind if I share your blog with my myspace group?
There’s a lot of people that I think would really appreciate your
content. Please let me know. Thanks
slot
Sage Advice About Malpractice Lawyer From A Five-Year-Old Malpractice Attorney
Heya! I’m at work browsing your blog from my new apple iphone!
Just wanted to say I love reading your blog and look forward to
all your posts! Keep up the outstanding work!