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
Excellent way of describing, and good paragraph to get data concerning my presentation focus,
which i am going to present in academy.
Howdy! I could have sworn I’ve been to this blog before but after browsing through many of
the articles I realized it’s new to me. Nonetheless, I’m certainly happy I found it
and I’ll be book-marking it and checking back regularly!
I really like reading through a post that can make people think.
Also, many thanks for allowing me to comment!
Amazing! Its genuinely awesome post, I have got much clear
idea concerning from this post.
Thanks , I have recently been searching for info approximately this subject for a long time and yours is the greatest I have came upon till now.
However, what concerning the conclusion? Are
you certain in regards to the source?
Hey there! This post could not be written any better!
Reading this post reminds me of my old room mate!
He always kept talking about this. I will forward this page to him.
Pretty sure he will have a good read. Thank you for sharing!
Right here is the perfect site for anybody who wants to find out
about this topic. You realize a whole lot its almost tough to argue with you (not that I actually will need to…HaHa).
You certainly put a new spin on a subject that’s been written about for
years. Great stuff, just wonderful!
No matter if some one searches for his vital thing, so
he/she wishes to be available that in detail, therefore that thing is maintained over here.
It is appropriate time to make some plans for the
future and it’s time to be happy. I’ve read this put
up and if I may just I wish to counsel you some attention-grabbing issues or advice.
Perhaps you can write subsequent articles relating to this article.
I desire to learn more issues about it!
Pretty great post. I simply stumbled upon your weblog and wished to say that I have really enjoyed
surfing around your blog posts. In any case I’ll be subscribing for your rss feed and I’m hoping
you write once more very soon!
This article is in fact a good one it helps new net viewers, who are wishing in favor
of blogging.
Howdy! This article could not be written any better!
Going through this article reminds me of my previous roommate!
He always kept talking about this. I’ll send this article to
him. Fairly certain he will have a very good read.
Thank you for sharing!
Great article! That is the type of info that are meant to be shared
around the web. Disgrace on Google for now not positioning
this publish upper! Come on over and visit my website . Thank you =)
Hello there, just became alert to your blog through Google, and
found that it is truly informative. I am gonna watch out for
brussels. I’ll be grateful if you continue this in future.
Lots of people will be benefited from your writing. Cheers!
Heya! I just wanted to ask if you ever have any problems
with hackers? My last blog (wordpress) was hacked and I ended up losing several weeks of hard work due to no backup.
Do you have any methods to stop hackers?
We’re a group of volunteers and starting a new scheme in our community.
Your website provided us with valuable information to work on.
You have done an impressive job and our entire
community will be thankful to you.
Hi all, here every one is sharing these kinds of knowledge,
so it’s nice to read this blog, and I used to go
to see this weblog daily.
Whoa! This blog looks just like my old one!
It’s on a totally different subject but it has pretty much
the same layout and design. Outstanding choice of colors!
Hello my loved one! I wish to say that this post
is amazing, nice written and include almost all
important infos. I would like to see more posts like this .
I love it when individuals come together and
share views. Great blog, continue the good work!
Very shortly this web page will be famous among all
blogging and site-building people, due to it’s fastidious articles or reviews
After I originally left a comment I appear to have clicked on the -Notify me when new
comments are added- checkbox and from now on every time a comment is added I recieve 4 emails with the same comment.
Is there an easy method you can remove me from that service?
Cheers!
Right away I am going to do my breakfast, after
having my breakfast coming again to read further news.
Hi there to all, how is everything, I think every one is
getting more from this web page, and your views are nice in favor of new viewers.
It is perfect time to make a few plans for the future and it’s time to be happy.
I have learn this submit and if I could I want to suggest you some attention-grabbing
issues or advice. Perhaps you could write next articles relating to this article.
I desire to learn more things about it!
I really love your blog.. Very nice colors & theme.
Did you build this web site yourself? Please reply back as I’m attempting
to create my very own blog and would like to know where you got this from or just what the theme is called.
Kudos!
Quality articles is the crucial to invite the people to pay
a quick visit the web site, that’s what this website is providing.
Keep this going please, great job!
Amazing! This blog looks exactly like my old one!
It’s on a totally different subject but it has pretty
much the same page layout and design. Wonderful choice of colors!
I need to to thank you for this excellent read!!
I definitely loved every little bit of it. I have got you saved as a favorite to check out new stuff you
post…