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
Its like you read my mind! You appear to know a lot about this, like you wrote the book
in it or something. I think that you could do with
a few pics to drive the message home a little bit, but other than that, this is
great blog. An excellent read. I’ll certainly be back.
Marketing Partnership: Driving Real Results for Your Business
Your success is my priority. As a small business owner, you need more than just marketing strategies; you need results that impact your revenue.
I am dedicated to delivering the outcomes that matter most:
growth and profitability.
At Blackbird Digital Marketing, I begin by understanding your unique challenges
and objectives. My tailored solutions focus on driving increased sales and attracting
new customers—everything you need to expand your business.
Профессиональный сервисный центр по ремонту принтеров в Москве.
Мы предлагаем: диагностика и ремонт принтеров
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
buying prescription drugs in mexico online: mexican pharma – medication from mexico pharmacy
mexican rx online
20 Things That Only The Most Devoted French Style Fridge
Freezers Fans Understand double door vs french door fridge (Jessica)
The Three Greatest Moments In ADHD Diagnosis
History How Do You Get A Diagnosis Of Adhd
Выше интернет-магазин “Мебель-шмммммммебель” делает отличное предложение широкий комплект мебели чтобы кухонь. У нас вы выкопаете шиздец необходимое для формирования уютного также многофункционального экстерьера http://www.tyfapao6kuhnishki.ru.
It’s not my first time to visit this web site, i am visiting this
site dailly and get good facts from here daily.
trouver un mГ©dicament en pharmacie: cialis prix – pharmacie en ligne france livraison belgique
https://pharmaciepascher.pro/# Pharmacie Internationale en ligne
http://vgrsansordonnance.com/# Viagra homme prix en pharmacie sans ordonnance
https://5stargamblingsites.com/
vente de mГ©dicament en ligne: Pharmacies en ligne certifiees – п»їpharmacie en ligne france
buying from online mexican pharmacy: mexico drug stores pharmacies – best online pharmacies in mexico
medicine in mexico pharmacies
pharmacie en ligne france livraison internationale: pharmacie en ligne pas cher – pharmacie en ligne france livraison internationale
Indisputable Proof That You Need Treehouse Loft Bed
tree House bunk bed (minecraftathome.com)
mexico drug stores pharmacies: medication from mexico – mexican drugstore online
mexico drug stores pharmacies
ラブドール エロYour body uses calories from food for walking,thinking,
R7 Casino зеркало является надежным способом получения доступа к любимому казино даже в случае блокировки основного сайта. Для обеспечения беспрепятственного доступа к сайту, казино R7 предлагает актуальные зеркала р7 казино рабочее зеркало
My brother suggested I might like this web site.
He was entirely right. This post truly made my day.
You cann’t imagine just how much time I had spent for this info!
Thanks!
Awesome article.
medicine in mexico pharmacies: mexican pharmacy – mexican rx online
best online pharmacies in mexico
Hi this is kinda 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 knowledge so I wanted to get advice from someone with experience.
Any help would be greatly appreciated!
I’m extremely impressed with your writing skills as well
as with the layout on your weblog. Is this a paid theme or did you modify it yourself?
Either way keep up the excellent quality writing, it is
rare to see a nice blog like this one these days.
pharmacie en ligne fiable Acheter Cialis pharmacie en ligne france fiable
I think this is among the most vital information for me.
And i am glad reading your article. But should remark on some general things,
The web site style is perfect, the articles is really great : D.
Good job, cheers
http://vgrsansordonnance.com/# Viagra homme prix en pharmacie sans ordonnance
buying prescription drugs in mexico: mexican pharmacy – medication from mexico pharmacy
mexican border pharmacies shipping to usa
Viagra vente libre allemagne: viagra en ligne – SildГ©nafil Teva 100 mg acheter
Pharmacie en ligne livraison Europe: pharmacie en ligne sans ordonnance – pharmacies en ligne certifiГ©es