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
I know this web page presents quality dependent content and extra stuff, is
there any other web page which offers these data in quality?
http://denemebonusuverensiteler.win/# bahis siteleri
Hi this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding experience so I wanted to get guidance from someone with experience.
Any help would be enormously appreciated!
http://denemebonusuverensiteler.win/# deneme bonusu
Appreciating the commitment you put into your blog and detailed information you present.
It’s great to come across a blog every once
in a while that isn’t the same out of date rehashed
material. Wonderful read! I’ve saved your site and I’m including your
RSS feeds to my Google account.
Evden Eve Nakliyat | Ev taşıma sürecinde dikkat edilmesi gereken noktaları bu kadar net bir şekilde açıkladığınız için teşekkürler. Kozcuoğlu Evden Eve Nakliyat olarak, müşterilerimize en iyi hizmeti sunmak için buradayız.
Howdy are using WordPress for your site platform?
I’m new to the blog world but I’m trying to get started and create
my own. Do you require any html coding knowledge to make your own blog?
Any help would be greatly appreciated!
You’ll Never Be Able To Figure Out This Cheap Leather Couches’s Tricks Cheap Leather Couches
Семейные расстановки Берта Хеллингера. https://rasstanovkiural.ru
balloons inexpensive https://helium-balloons-dubai.com
yasal slot siteleri: bonus veren casino slot siteleri – deneme bonusu veren siteler
Бесплатный промокод Fonbet https://kmural.ru/news_importer/inc/aktualnue_promokodu_bukmekerskoy_kontoru_fonbet.html
Бесплатные промокоды Fonbet предоставляют пользователям возможность получить различные бонусы, включая бесплатные ставки. Примером такого промокода является ‘GIFT200’, который активирует фрибеты для новых игроков. Эти промокоды делают игру более увлекательной и увеличивают шансы на выигрыш.
https://xn--vb0bp4v22gmgm5i5v1b.mystrikingly.com/blog/cfe88a3735c
https://slotsiteleri.bid/# deneme bonusu veren slot siteleri
Meds information leaflet. Long-Term Effects.
ropinirole rx
Best what you want to know about medicine. Read information here.
Hi there, I found your site by the use of Google while looking for a comparable matter,
your website got here up, it looks good. I’ve bookmarked it
in my google bookmarks.
Hello there, just was alert to your weblog via Google, and located that
it’s truly informative. I am gonna be careful for brussels.
I’ll be grateful if you continue this in future.
Numerous other folks shall be benefited out of your writing.
Cheers!
Привет!
Мы можем предложить документы техникумов
respire.localoco.net/dwqa-question/kupit-diplom-997743fbj
https://denemebonusuverensiteler.win/# bahis siteleri
I absolutely love your website.. Very nice colors & theme.
Did you make this web site yourself? Please reply back as I’m
trying to create my own personal website and want to find out where
you got this from or exactly what the theme is called.
Kudos!
Профессиональный сервисный центр по ремонту холодильников и морозильных камер.
Мы предлагаем: ремонт холодильника
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
First of all I want to say great blog! I had a quick question in which I’d like to ask
if you do not mind. I was interested to know how you
center yourself and clear your mind prior to writing.
I’ve had a tough time clearing my mind in getting
my thoughts out there. I do take pleasure in writing however it just seems like
the first 10 to 15 minutes tend to be wasted simply just trying to figure out how to begin. Any suggestions or hints?
Thanks!
http://slotsiteleri.bid/# slot bahis siteleri
Ev Taşıma | Taşınma sürecinde dikkat edilmesi gereken noktaları bu kadar detaylı açıklamanız çok faydalı olmuş. Kozcuoğlu Ev Taşıma olarak, müşterilerimize bu konuda rehberlik etmekten memnuniyet duyuyoruz.
What a information of un-ambiguity and preserveness of
valuable knowledge regarding unexpected emotions.
Definitely believe that which you stated.
Your favourite justification appeared to be on the net the simplest factor to
take into accout of. I say to you, I definitely get annoyed whilst people
think about issues that they just do not recognize about.
You managed to hit the nail upon the highest as
smartly as defined out the whole thing with no need side-effects , other people can take
a signal. Will probably be back to get more. Thanks
I am no longer sure the place you’re getting your information, but good topic.
I needs to spend a while learning more or figuring out more.
Thank you for fantastic info I was on the lookout for this information for my mission.
order helium balloons with delivery https://helium-balloons-dubai.com
http://slotsiteleri.bid/# yasal slot siteleri
order helium balloons with delivery balloons price
air helium balloons Dubai balloons buy balloons