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
Как получить диплом техникума с упрощенным обучением в Москве официально
Как купить аттестат 11 класса с официальным упрощенным обучением в Москве
вызов нарколога на дом краснодар http://www.bisound.com/forum/showthread.php?p=1217304#post1217304/ .
priligy maxpharm: buy dapoxetine online – Priligy tablets
This was very enlightening. For more, visit http://reviewlamdepcongtam.blogspot.com/2024/11/review-benh-vien-da-lieu-trung-uong.html
нарколог на дом нарколог на дом .
Всем привет! Может кто знает, где почитатьполезные блоги о недвижимости? Пока нашел – https://glavtorgmsk.ru
buy amoxicillin over the counter uk: amoxicillin 500mg for sale uk – amoxicillin 500mg capsules price
врач нарколог на дом платный familyportal.forumrom.com/viewtopic.php?id=28566 .
where buy cheap clomid no prescription: rex pharm – buy generic clomid
Thanks for the helpful article. More like this at http://linbeautyspatintuc.blogspot.com/2024/10/review-ngoc-oanh-beauty-spa-quan-tay-ho.html
Всем привет! Может кто знает, где почитатьразные блоги о недвижимости? Сейчас читаю – https://glavtorgmsk.ru
You are so awesome! I do not suppose I’ve truly read through
something like that before. So nice to find another person with a few original
thoughts on this subject matter. Seriously.. many thanks
for starting this up. This site is one thing that’s needed on the internet,
someone with a bit of originality!
Your mode of telling everything in this article is genuinely good,
all can effortlessly be aware of it, Thanks a lot https://www.provenexpert.com/en-gb/pub028/
This was quite informative. For more, visit http://linbeautyspatintuc.blogspot.com/2024/10/review-bao-viet-spa-quan-ba-inh-trai.html
Thanks for all your efforts on this blog. My mom loves making time for
research and it is simple to grasp why. All of us notice
all about the compelling means you make effective guidelines via your blog
and in addition recommend response from visitors on this
area of interest so our own girl is discovering a lot of
things. Enjoy the remaining portion of the new year.
You’re performing a fabulous job.[X-N-E-W-L-I-N-S-P-I-N-X]I am really inspired with your
writing talents and also with the layout on your weblog.
Is this a paid theme or did you modify it yourself?
Either way keep up the nice quality writing,
it is rare to see a nice weblog like this one nowadays.
Hey there, I think your site might be having browser compatibility issues.
When I look at your blog in Chrome, it looks fine but when opening in Internet Explorer, it has
some overlapping. I just wanted to give you a quick heads up!
Other then that, amazing blog!
I was recommended this web site by my cousin. I am not sure whether this post is written by him as no one else know
such detailed about my trouble. You are incredible! Thanks!
частный нарколог на дом setter.borda.ru/?1-7-0-00000673-000-0-0-1730729894 .
Hello there, I found your site via Google while
searching for a related subject, your site came up, it seems to be good.
I have bookmarked it in my google bookmarks.
Hi there, simply changed into aware of your blog thru Google,
and found that it’s really informative. I am gonna be careful for brussels.
I will be grateful should you continue this in future.
A lot of other people will probably be benefited from your writing.
Cheers!
вызов нарколога на дом http://zavitai.mybb.social/viewtopic.php?id=89 .
Because the admin of this web site is working, no hesitation very quickly it will
be renowned, due to its feature contents.
Thanks for the valuable insights. More at http://linbeautyspatintuc.blogspot.com/2024/10/review-drphuong-thao-beauty-spa-quan-11.html
вызов нарколога на дом вызов нарколога на дом .
where to get cheap clomid prices: generic clomid – buying cheap clomid
выезд нарколога на дом http://familyportal.forumrom.com/viewtopic.php?id=28566 .
Hey there would you mind sharing which blog platform you’re using?
I’m looking to start my own blog soon but I’m having a
tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems
different then most blogs and I’m looking for something unique.
P.S Sorry for being off-topic but I had to ask!
I’m not sure where you are getting your information, but great topic.
I needs to spend some time learning much more or understanding more.
Thanks for fantastic info I was looking for this info for my mission.
Somebody necessarily lend a hand to make significantly posts I would state.
This is the very first time I frequented your web page and to this
point? I amazed with the research you made to make this particular publish incredible.
Great process!
Как официально купить аттестат 11 класса с упрощенным обучением в Москве