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
Good blog you have got here.. It’s hard to find high quality writing
like yours these days. I seriously appreciate people like you!
Take care!!
Heya i am for the first time here. I found this board and I find It
truly useful & it helped me out a lot. I hope to give something back and help others like you
aided me.
Thanks to my father who stated to me concerning this web site, this webpage is in fact amazing.
I feel that is one of the so much important information for me.
And i’m happy studying your article. But wanna remark on few common things, The web site taste is
wonderful, the articles is in reality great : D. Excellent process, cheers
You can certainly see your expertise within the article you write.
The sector hopes for more passionate writers like you who aren’t afraid to mention how they
believe. At all times go after your heart.
Quality posts is the important to interest the users
to visit the site, that’s what this web page is providing.
Hey there! This is my 1st comment here so I just wanted to give a quick shout out and say I truly enjoy reading your posts.
Can you recommend any other blogs/websites/forums that go
over the same subjects? Thanks a lot!
Great article! We will be linking to this particularly great post on our site.
Keep up the great writing.
What’s up to all, it’s really a nice for me to pay a visit this web site, it contains useful
Information.
When some one searches for his required thing,
so he/she needs to be available that in detail, therefore that thing is maintained over
here.
Hello! I simply want to offer you a big thumbs up for the excellent information you have got here on this post.
I will be coming back to your website for more soon.
Amazing issues here. I’m very glad to peer your article.
Thanks so much and I’m taking a look forward to contact you.
Will you please drop me a e-mail?
Beim Hotelaufenthalt hingegen befindet sich der Gast nach Verlassen seines
Zimmers sofort in der Öffentlichkeit. Der gesamte Aufenthalt lässt sich lockerer und ungezwungener gestalten.
Über die obigen Angebote und die Suchmaske können Sie viele günstige Skireisen miteinander vergleichen. Bunt Reisezielen bietet den Vorteil, dass für jede person der richtige Skiurlaub 2013 vorhanden ist.
Urlaub im Schnee ist meist ein sportlich-aktiver Urlaub und der Tagesablauf in den Skiferien wird weitgehend von Abfahrtski, Skilanglauf und Rodeln bestimmt.
Die einen freuen sich auf einen sonnigen Sommerurlaub, andere können den Winterurlaub kaum erwarten und buchen schon Monate vorher eine attraktive Skireise.
Das gilt besonders für einen Familienurlaub mit mehreren Personen. Diese ist
in vielen Fällen dabei Aufenthalt Hand in Hand gehen Ferienhaus verbunden. Bei der Auswahl
einer Unterkunft für den Skiurlaub haben Ferienwohnungen oder Ferienhäuser einige Vorteile gegenüber
dem Hotel. Neben dem spürbar günstigeren Preis bieten Wohnungen und Ferienhäuser deutlich mehr Flexibilität, denn für
die Mahlzeiten gibt es beispielsweise im Gegensatz zur Halb-
oder Vollpension im Hotel keinerlei Vorgaben.
hello there and thank you for your info – I have definitely
picked up something new from right here. I did however expertise several technical points using
this web site, since I experienced to reload the site many times previous to I could get it to load correctly.
I had been wondering if your web host is OK?
Not that I’m complaining, but slow loading instances times will often affect your placement in google and can damage your quality score
if advertising and marketing with Adwords. Well I am
adding this RSS to my email and could look out for much more of your respective fascinating content.
Ensure that you update this again soon.
You have made some really good points there. I looked on the
net for more info about the issue and found most individuals
will go along with your views on this site.
What’s up, after reading this awesome piece of writing i am too delighted to share my knowledge here with mates.
It’s awesome to go to see this web site and reading the views of
all colleagues about this post, while I am also zealous of getting knowledge.
Thank you a lot for sharing this with all people you actually know what you are speaking about!
Bookmarked. Please also talk over with my web site =).
We may have a link alternate arrangement between us
Aw, this was an extremely good post. Taking the time and actual effort to create
a superb article… but what can I say… I put things off a whole lot and don’t manage to get nearly
anything done.
I have read so many articles about the blogger lovers but this article is in fact a pleasant piece of writing,
keep it up.
It is perfect time to make some plans for the future and it is time to be happy.
I’ve read this post and if I could I want to
suggest you few interesting things or tips. Perhaps you can write next articles referring to
this article. I desire to read more things about it!
Hello, i believe that i noticed you visited my website thus i got here to go back the want?.I’m trying to find
things to enhance my site!I assume its good enough to use some of
your concepts!!
Magnificent site. Lots of useful info here. I am sending it to several friends
ans additionally sharing in delicious. And certainly, thank
you to your effort!
Generally I do not learn post on blogs, but I wish to say that this
write-up very compelled me to try and do so!
Your writing taste has been surprised me. Thanks, very nice post.
Great post.
Great article.
Can I just say what a comfort to find an individual who genuinely understands what they are
discussing on the net. You actually know how to bring an issue to light and
make it important. More and more people really need to check this
out and understand this side of the story.
I was surprised you’re not more popular because you surely have the gift.
Hello, just wanted to say, I loved this article.
It was practical. Keep on posting!
Great blog you have here but I was curious about if you
knew of any forums that cover the same topics discussed in this article?
I’d really like to be a part of group where I can get opinions
from other knowledgeable people that share the same interest.
If you have any suggestions, please let me know.
Many thanks!
I’m impressed, I must say. Seldom do I come across a blog that’s both equally educative and engaging, and without a doubt, you have hit the nail on the head.
The issue is an issue that too few people are speaking intelligently about.
Now i’m very happy that I came across this in my hunt for something regarding this.