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 blog frequently and I seriously thank you for
your content. The article has really peaked my interest.
I will bookmark your site and keep checking for new details about once per week.
I subscribed to your RSS feed too.
Thank you, I have recently been searching for info approximately this topic for a long time and yours is the
greatest I’ve came upon so far. But, what in regards to the bottom line?
Are you certain in regards to the supply?
What’s up every one, here every one is sharing such know-how, so it’s nice to
read this web site, and I used to visit this website all the
time.
I am really enjoying the theme/design of your website.
Do you ever run into any web browser compatibility problems?
A couple of my blog audience have complained
about my site not working correctly in Explorer but looks great in Chrome.
Do you have any recommendations to help fix this
issue?
Hi there! Do you use Twitter? I’d like to follow you if that would
be okay. I’m definitely enjoying your blog and look forward to new updates.
Excellent web site. Lots of helpful information here. I am sending it to a few friends ans additionally
sharing in delicious. And naturally, thanks in your effort!
Excellent post. I was checking continuously this blog and I am impressed!
Extremely helpful info particularly the last part 🙂 I care for such info a lot.
I was looking for this certain info for a long time. Thank you and good luck.
Wonderful work! That is the type of information that are meant
to be shared around the net. Disgrace on the seek engines for now not positioning this post
higher! Come on over and consult with my website .
Thanks =)
Do you have a spam issue on this blog; I also am a blogger, and I was
wanting to know your situation; many of us have created some nice methods and we are
looking to swap solutions with other folks, be sure to shoot me
an email if interested.
It іѕs in oint of fact a great and helpful piece of info.
I’m haρpy that you simρly shared this useful іnfo with us.
Please keep us informed likе this. Thank yⲟu for sharing.
Visit my page: レンタル楽器, V.Gd,
Hmm it seems like your website ate my first comment
(it was super long) so I guess I’ll just sum it up
what I submitted and say, I’m thoroughly enjoying your blog.
I as well am an aspiring blog writer but I’m still
new to the whole thing. Do you have any tips and hints for inexperienced blog writers?
I’d definitely appreciate it.
My spouse and I stumbled over here from a different page and thought
I might as well check things out. I like what I see so now i am
following you. Look forward to finding out about your web page for a
second time.
I’ve learn some excellent stuff here. Definitely value bookmarking for revisiting.
I wonder how so much effort you place to make this type of
great informative site.
Excellent post. Keep writing such kind of info on your page.
Im really impressed by your site.
Hello there, You have performed an excellent job. I will definitely digg
it and for my part recommend to my friends. I am confident they’ll be benefited
from this website.
What’s up to all, how is everything, I think every one is getting
more from this web site, and your views are fastidious in favor of new people.
Wie unterscheiden sich Thermorollen von Normalpapier Kassenrollen und wo werden diese eingesetzt?
Bei Bonrollen aus Thermopapier ist eine Seite des Papier thermoaktiv beschichtet,
dass an der Zeit sein verfärbt sich unter der Einwirkung von Hitze.
Bei Bonrollen aus Normalpapier ist das Papier nicht
beschichtet, der Druck erfolgt wenn man alles berücksichtigt über einen Nadeldrucker, der widerum
ein Farbband benötigt. Bei einem Thermodrucker erzeugt der Druckkopf diese
Hitze an den zu bedruckenden Stellen, so, dass Schrift
oder Grafiken in Erscheinung treten. Aufgrund der Hitzeempfindlichkeit
von Thermopapier ist es überall dort ungeeignet, wo es außerhalb des Druckers Hitze ausgesetzt wird, z.B.
Rand eines vorgewärmten Tellers in der Küche zu legen. Was kann ich unter
Kassenrollen mit Lastschrifttext verstehen? Diesen unterschreibt Ihr Kunde bei der Bezahlung mit seiner EC-Karte.
Bei Bonrollen mit Lastschrifttext befindet sich auf der Rückseite des Papier ein allgemeingültiger Lastschrifttext.
Sofern Ihre Kunden stets eine PIN zur Authorisierung eingeben müssen, benötigen Sie keine Rollen mit Lastschrifttext.
Gemeinhin sind diese Rollen 57mm breit und aus Thermopapier.
Thankfulness to my father who stated to me regarding this website,
this webpage is in fact amazing.
It’s amazing to pay a visit this web page and reading the views of all mates regarding this paragraph, while I am also
eager of getting know-how.
Wow! At last I got a website from where I be capable of genuinely
obtain useful information regarding my study and knowledge.
If you are going for most excellent contents like me, just
visit this web page all the time for the reason that it provides quality contents, thanks
Interesting blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple tweeks would really make my blog shine.
Please let me know where you got your design.
Cheers
Hello there! This post could not be written any better!
Looking through this article reminds me of my previous roommate!
He continually kept preaching about this. I’ll forward this article to
him. Pretty sure he’s going to have a very good read. Thanks for sharing!
I absolutely love your blog and find the majority of your post’s
to be just what I’m looking for. Does one offer guest writers to write content for you personally?
I wouldn’t mind publishing a post or elaborating on many of
the subjects you write related to here. Again, awesome website!
Howdy! I could have sworn I’ve been to this
site before but after reading through some of the post I realized it’s new to me.
Anyhow, I’m definitely delighted I found it and I’ll be bookmarking and checking back often!
This is the perfect webpage for everyone who
wishes to find out about this topic. You understand a whole lot its
almost tough to argue with you (not that I really would want to…HaHa).
You certainly put a brand new spin on a topic that’s been written about for many years.
Great stuff, just wonderful!
Hi! I understand this is kind of off-topic but I needed to ask.
Does managing a well-established website such as yours require a large amount
of work? I’m brand new to writing a blog however I
do write in my journal on a daily basis. I’d like to start a blog so I will be able
to share my experience and feelings online. Please let me know if you have any kind of ideas or tips for brand new aspiring bloggers.
Thankyou!
Hello! I could have sworn I’ve visited this website before but after browsing through many of
the posts I realized it’s new to me. Anyways, I’m definitely
pleased I found it and I’ll be bookmarking it and checking back frequently!
I know this if off topic but I’m looking into starting my own blog and was curious what all is required to get setup?
I’m assuming having a blog like yours would cost a pretty penny?
I’m not very web savvy so I’m not 100% certain. Any
recommendations or advice would be greatly appreciated. Thank you
I like this post, enjoyed this one regards for posting.
It’s appropriate time to make some plans for the future and it is
time to be happy. I have learn this submit and if I
may I want to counsel you few attention-grabbing issues or
suggestions. Perhaps you can write subsequent articles regarding this article.
I want to read more things approximately it!