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
Authentic HGH Rewards (NewULife Hgh Gel Not Examined Yet).
My web page … types of disability benefits in california
Drugs information sheet. What side effects can this medication cause?
order keflex
Actual information about medicines. Get here.
What a information of un-ambiguity and preserveness of precious knowledge on the topic of unpredicted emotions.
Guide To Online Shopping Uk Discount: The Intermediate Guide For Online Shopping Uk Discount online shopping uk discount (pipewiki.org)
I’m impressed, I must say. Seldom do I come across a blog that’s equally educative and amusing, and let
me tell you, you have hit the nail on the head. The problem is
something not enough folks are speaking intelligently about.
I’m very happy I found this during my hunt for something regarding this.
I’m impressed, I have to admit. Rarely do I encounter a
blog that’s both educative and amusing, and without a doubt, you’ve hit the nail on the head.
The problem is something not enough people are speaking intelligently about.
Now i’m very happy I came across this in my search
for something regarding this.
NewULife Ranks. Invite how to report content analysis results my
NewUlife Review.
Hello Dear, are yⲟu genuinely visiting this website regularly, if ѕo then you wіll without doubt obtain fastidious know-how.
Check out myy рage :: otoslot4d
Drug prescribing information. Generic Name.
can you buy prochlorperazine no prescription
Actual news about drug. Read information now.
Great info. Lucky me I discovered your blog by chance (stumbleupon).
I’ve bookmarked it for later!
https://fisketavling.nu/cb-profile/pluginclass/cbblogs?action=blogs&func=show&id=332
Drug information sheet. Brand names.
can i get cheap zithromax without a prescription
Everything trends of pills. Get here.
Nine Things That Your Parent Taught You About Car Accident Lawsuit Car Accident
Their production exclusively uses organic hemp.
my web page … Bookmarks
http://o91707v5.beget.tech/2024/06/30/sem-kvartiry-na-den-kak-podobrat-horoshiy-variant.html
Pills information. Long-Term Effects.
can you buy zithromax pills
Some what you want to know about medicines. Get information now.
What’s up Dear, are you in fact visiting this web site daily, if so then you
will absolutely obtain good knowledge.
Drugs information sheet. Brand names.
can i purchase generic nolvadex without prescription
Best trends of meds. Get here.
http://o91707v5.beget.tech/2024/06/30/sem-kvartiry-na-den-kak-podobrat-horoshiy-variant.html
Medicine information. Drug Class.
ventolin no prescription
All about medication. Read now.
https://www.southjerseyonline.net/component/comprofiler/pluginclass/cbblogs.html?action=blogs&func=show&id=261
Both mushrooms products are immune home builders.
My webpage Bookmarks (Elouise)
10 Things You Learned In Preschool That’ll Help You Understand Drip Filter Coffee Machine drip filter coffee machines
What’s The Job Market For Window Repair Near Me
Professionals Like? Window repair near me
10 Quick Tips About Upvc Windows Repair upvc window repairs near me
Howdy! I realize this is kind of off-topic however I needed to ask.
Does building a well-established website such as yours take a large amount of work?
I’m brand new to running a blog but I do write in my journal on a daily basis.
I’d like to start a blog so I will be able to share my
experience and feelings online. Please let me know if
you have any kind of suggestions or tips for new aspiring
blog owners. Thankyou!
Как выбрать лучшую клинику стоматологии, ознакомиться.
Уникальные методики лечения зубов, профессиональный уход за зубами.
Как избежать боли при лечении зубов, предлагаем.
Что нужно знать о здоровье полости рта, эффективные советы стоматолога.
Как избежать проблем с зубами, предлагаем.
Как выбрать хорошего стоматолога, эффективные методики стоматологии.
Что делать при кровоточащих деснах, предлагаем.
дитячі стоматологи дитячі стоматологи .
Are You Responsible For An Medical Malpractice Attorney Budget?
12 Top Ways To Spend Your Money Medical Malpractice Law Firms
I am actually pleased to glance at this blog posts which consists of lots of helpful data, thanks for providing such information.
My web page – Judi Bola Dor77
Hire Truck Accident Attorney: 11 Things You’re Leaving Out Truck Accident
Lawsuits, Omise.Honesta.Net,