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
Having read this I thought it was very enlightening.
I appreciate you taking the time and energy to put this article
together. I once again find myself personally spending a significant amount of time both reading and posting comments.
But so what, it was still worth it!
My brother suggested I might like this blog.
He was totally right. This post truly made my day. You can not imagine
just how much time I had spent for this information! Thanks!
I’ve been exploring for a little bit for any high quality articles or
weblog posts in this sort of space . Exploring in Yahoo I
eventually stumbled upon this site. Studying this info So i am glad to exhibit
that I’ve a very just right uncanny feeling I found out exactly what I needed.
I such a lot without a doubt will make certain to don?t fail to remember this web site and provides it a glance regularly.
Wow, fantastic blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your site is magnificent, let
alone the content!
Its like you read my mind! You seem to know a lot about this, like you wrote
the book in it or something. I think that you could do with a few pics to drive the message
home a bit, but other than that, this is fantastic blog.
An excellent read. I’ll definitely be back.
Hey there, You have done an incredible job. I’ll certainly digg it and personally suggest to my friends.
I am sure they will be benefited from this web site.
I visited many blogs except the audio quality
for audio songs current at this website is actually
superb.
Excellent article. Keep writing such kind of information on your page.
Im really impressed by your blog.
Hi there, You’ve done a fantastic job. I will certainly digg it and for my part suggest to my friends.
I am confident they will be benefited from this website.
Can I simply say what a comfort to find somebody that
genuinely knows what they’re discussing on the net.
You actually know how to bring an issue to light and make it
important. More people really need to read this and
understand this side of your story. I was surprised that you aren’t more popular since you definitely have the gift.
Hello, yes this article is really good and I have learned lot of things from it regarding blogging.
thanks.
You can certainly see your expertise in the article you write.
The world hopes for more passionate writers like you who are not afraid to mention how they believe.
Always follow your heart.
Hi, i think that i saw you visited my site thus i came to return the favor?.I’m
attempting to in finding things to enhance my site!I assume its adequate to use a few of your concepts!!
Can you tell us more about this? I’d care to find out more details.
My partner and I stumbled over here coming from a different web address and thought I should check things out.
I like what I see so now i’m following you. Look forward to finding out about your
web page repeatedly.
I do accept as true with all the ideas you
have offered to your post. They’re very convincing and will certainly work.
Nonetheless, the posts are very short for beginners.
May just you please extend them a little from subsequent time?
Thank you for the post.
Generally I do not learn post on blogs, but I wish to say that this
write-up very forced me to take a look at and do so! Your writing taste has been amazed me.
Thank you, quite nice post.
The other day, while I was at work, my cousin stole my apple ipad and tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views.
I know this is totally off topic but I had to share it with someone!
An interesting discussion is definitely worth comment. There’s no doubt that that you should publish more on this subject matter, it might not be a
taboo subject but usually people do not speak about these issues.
To the next! Many thanks!!
Fastidious answer back in return of this matter with solid arguments and describing the whole
thing on the topic of that.
What i do not understood is in reality how you’re not actually
a lot more smartly-liked than you may be now. You’re very intelligent.
You already know thus considerably relating to this subject, made
me in my opinion imagine it from a lot of numerous angles.
Its like men and women don’t seem to be involved except it’s something to accomplish with Girl
gaga! Your personal stuffs nice. All the time maintain it up!
I’m not sure where you are getting your info,
but great topic. I needs to spend some time learning much more or understanding more.
Thanks for great info I was looking for this information for my mission.
Thankfulness to my father who shared with me on the topic of this website,
this weblog is truly remarkable.
Thanks for sharing your thoughts on judi slot online indonesia.
Regards
I’m not sure why but this site is loading very slow for me.
Is anyone else having this issue or is it a problem
on my end? I’ll check back later and see if the problem still exists.
Beim Mercat de I‘Olivar gehören wir definitiv zu Wiederholungsbesuchern bzw.
Wiederholungskäufern! Warum? Für uns ist es nicht der Geruch und die Farbenprächtigkeit von gerade geerntetem
Obst und Gemüse, die Vielfalt und Frische der Meeresfrüchte und Fische, sondern auch das Treiben der Palmeser.
Ein Schwatz zwischen Käufer und Verkäufer gehört obligatorisch dazu,
die Wartenden stören sich nicht dran, obwohl sie mehrere Minuten länger
warten müssen. Über 30 Händler bieten – in einer separaten Halle – frisch gefangenen Fisch und Meeresfrüchte in einer Vielfalt an,
die es auf der Insel nur hier gibt. Auf Eis geschichtet warten Doraden, Goldmakrelen, Wolfsbarsche, Schwertfische,
Garnelen und Langusten, Austern oder auch schon fast bedrohlich aussehende Seeteufel auf ihre Käufer.
Man mag sich bei manchen dieser Kreaturen ganz und gar unmöglich vorstellen, dass diese in den balearischen Gewässern heimisch sind.
Im weiteren Bereich der Markthalle ganz unten bieten überwiegend Gemüse-,
Käse-, Fleischhändler ihre frischen und erstklassigen Waren feil.
Man findet jedoch auch eine Konditorei, Stände mit Trockenfrüchten, Süßigkeiten oder Gewürzen, einen Korbwarenladen, ein Blumengeschäft
und diverse Weinhändler. Im oberen Stockwerk ergänzt ein Mercadona-Markt das Angebot.
Now I am going away to do my breakfast, later than having my breakfast coming yet again to read other news.
Very good article. I’m facing many of these issues as well..
Greetings! Very helpful advice within this post!
It’s the little changes that make the biggest changes.
Thanks for sharing!
Greetings! I know this is kind of off topic but I was wondering
if you knew where I could find a captcha plugin for
my comment form? I’m using the same blog platform as yours
and I’m having problems finding one? Thanks a lot!
An impressive share! I’ve just forwarded this
onto a friend who was doing a little homework
on this. And he in fact ordered me breakfast simply because I found it for him…
lol. So let me reword this…. Thanks for the meal!!
But yeah, thanks for spending time to discuss this issue here on your website.