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
This was a wonderful post. Check out https://hackaday.io/muabongmoiorg for more
Предлагаем услуги профессиональных инженеров офицальной мастерской.
Еслли вы искали ремонт телефонов infinix рядом, можете посмотреть на сайте: ремонт телефонов infinix цены
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
A Cheat Sheet For The Ultimate On Coffee Machines Best
best bean to cup coffee Machine Under 500
перепланировка квартиры проектные организации перепланировка квартиры проектные организации .
Предлагаем услуги профессиональных инженеров офицальной мастерской.
Еслли вы искали ремонт телефонов honor в москве, можете посмотреть на сайте: ремонт телефонов honor в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Aw, this was an incredibly nice post. Finding the time and actual effort to produce a really good article…
but what can I say… I put things off a lot and don’t seem to get anything done.
Аttrɑctivee secftion ߋff content. Ӏ jjst styumbled upoon yoᥙr blоkg
annd iin acⅽеѕsoon capitl too assert tht I gett inn fact eenjoyed acount our bpog posts.
Anny wayy I wiill bbe subscribing too yiur augment aand evcen I achievvement yoou access
ϲonsustentⅼy qսickly.
Hеrre iis myy webpage; خرید آنلاین با تخفیف
I had a great experience with my last move, thanks to queens moving companies
you are actually a good webmaster. The site loading pace is incredible.
It seems that you’re doing any distinctive trick.
Moreover, The contents are masterpiece. you’ve performed a magnificent job on this subject!
So glad I found out about local movers through local movers westchester ## – they made everything
Предлагаем услуги профессиональных инженеров офицальной мастерской.
Еслли вы искали ремонт телефонов infinix цены, можете посмотреть на сайте: ремонт телефонов infinix адреса
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
can i buy clomid online: where to buy generic clomid without rx – can you buy clomid no prescription
dark website dark internet darkmarkets
Very helpful read. For similar content, visit https://disqus.com/by/muabongmoiorg/about/
This web site definitely has all of the information I needed about this subject and didn’t know who to ask.
O TechTudo reuniu abaixo cinco dicas e sites que ajudam a fazer download de vídeos do YouTube facilmente no PC, notebook
ou celular, sem precisar instalar nada no dispositivo.
Предлагаем услуги профессиональных инженеров офицальной мастерской.
Еслли вы искали ремонт телефонов infinix адреса, можете посмотреть на сайте: ремонт телефонов infinix
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
My friends recommended #_## anykeyword###_ when I was looking for moving services—they were spot moving company brooklyn
Ɗoes yoᥙr website havve a contact page? I’m having
problems locating itt Ƅut, I’d like to send y᧐u an e-maіl.
I’ve got some ideas for your blog you might bee interesteԁ
in hearing. Eithdr way, great sіte and I ⅼook forward to seeіng itt grow over time.
Aⅼⅼso visit my web ƅlߋg; sewа alat berat (http://classicalmusicmp3freedownload.com/)
Thanks for the great explanation. More info at Agence SEO experte
Thanks for the thorough article. Find more at https://gitlab.aicrowd.com/muabongmoiorg
электрокарниз москва электрокарниз москва .
Anyone have experiences with long distance movers in Sarasota? I’ve heard good things about long distance movers sarasota lately
Looking forward to sharing more about how well organized everything was thanks to ####ANYKEYWORD#! moving company brooklyn
Hey I know this is off topic but I was wondering if you knew of any widgets I could
add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some time
and was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy reading your blog and
I look forward to your new updates.
Everyone loves what you guys tend to be up too. This sort of clever work
and exposure! Keep up the excellent works guys I’ve added
you guys to my personal blogroll.
prednisone 5mg over the counter PredniBest prednisone 50 mg tablet cost
Looking forward to sharing this experience with colleagues; we should all be better prepared!!! CPR Training
Just wanted to share my positive experience with long distance movers from Sarasota at long distance moving companies
ciprofloxacin 500 mg tablet price CiPharmDelivery ciprofloxacin generic price