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
Mobile vaporizers use batteries as a source of power.
my web blog :: what beers are vegan uk
First off I want to say wonderful blog! I had a quick question which I’d like to ask
if you do not mind. I was interested to find out how you
center yourself and clear your head prior to writing. I’ve had a tough time clearing my thoughts in getting my ideas out.
I truly do enjoy writing however it just seems like the first 10 to 15 minutes are generally lost simply just trying to figure out how to begin. Any suggestions
or tips? Thanks!
An artificial USP human development hormonal agent (somatropin).
Here is my web blog – content analysis report example
Asbestos Law-related victims have a restricted amount
of time to file lawsuits under the law called statutes of limitation. Once a lawsuit has been filed,
both sides’ lawyers gather evidence in a process called discovery.
Meds information. Cautions.
order generic nolvadex without dr prescription
Everything news about medicines. Read now.
Meds information. Brand names.
buy dutasteride without insurance
Actual information about medicines. Read now.
Let me reveal you in my evaluation of NewULife.
my website :: Bookmarks
I’m the proprietor driver of GOPARTYBUS party bus perth 20 seater.
Pills information for patients. Effects of Drug Abuse.
where can i buy kamagra no prescription
Some news about medicament. Get now.
Antoine Griezmann https://antoine-griezmann.prostoprosport-br.com Futebolista frances, atacante e meio-campista do Atletico de Madrid. Jogador e vice-capitao da selecao francesa, integrante da selecao – campea mundial 2018. Medalhista de prata no Europeu de 2016 e no Mundial de 2022.
What’s The Job Market For Online Shopping Sites Professionals?
jolie papier Online shop uk amazon
Meds information for patients. Cautions.
cost of generic actos without prescription
All what you want to know about medicament. Read here.
An artificial USP human development hormonal
agent (somatropin).
Feel free to surf to my web-site what is data decay
What Is The Future Of Workers Compensation Law Be Like In 100 Years?
Workers’ Compensation
Medicines information. Drug Class.
how to buy generic dilantin pill
All news about medicine. Read now.
оснащение конференц зала оснащение конференц зала .
Authentic HGH Perks (NewULife Hgh Gel Not Checked
Yet).
Here is my web-site: what is considered a disability in ontario
15 Amazing Facts About Online Shopping Figures Uk The Words You’ve Never Learned
best luxury online shopping Sites uk (support.parsdata.com)
I couldn’t refrain from commenting. Perfectly
written!
Players in this attractive round from the chance to design and personalize their avatars while exploring distinct understood landscapes. The uncoloured of avatar world is to emplane on quests, go out with other players, and upon your avatar’s unique abilities. This game provides an mind-boggling incorporate of customization, investigation, and community.
Hello very cool web site!! Man .. Beautiful ..
Superb .. I’ll bookmark your site and take the feeds
additionally…I am glad to seek out a lot of helpful
information right here in the put up, we’d like develop
extra strategies on this regard, thanks for sharing.
My web page … best SEO services
What is New U Life SOMADERM Transdermal Gel.
Feel free to surf to my web page; AI Headshot Generator
Ten Things You Shouldn’t Share On Twitter Undiagnosed Adhd In Adult Women; https://Exbb.Info/Community/Rd.Php?Https://Ring-Penn.Blogbright.Net/The-Top-Reasons-People-Succeed-In-The-Add-In-Adult-Women-Industry,
Kylian Mbappe is a French professional footballer who plays as a forward for Paris Saint-Germain and the French national team. Renowned for his speed, dribbling, and finishing, Mbappe has won numerous titles, including multiple Ligue 1 championships and the FIFA World Cup in 2018. Find out more about him here – https://kylianmbappe.paris-saint-germain-ar.com/
Pills information leaflet. What side effects?
buying cheap atarax online
Everything trends of drugs. Get now.
See What Birth Injury Lawyer Tricks The Celebs Are Using Birth Injury
リアル ラブドールespecially with children,also tends to correlate with height,
NewULife Ranks. Welcome to my NewUlife Testimonial.
Feel free to surf to my web site: types of disability uk
Thanks for some other informative web site. The place else could I get that kind of information written in such a
perfect manner? I’ve a project that I’m simply now operating
on, and I’ve been at the glance out for such info.
современный конференц зал oborudovanie-konferenc-zalov11.ru .