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
Yes! Finally something about jadwal sabung ayam sv388 hari ini.
Very soon this web site will be famous amid all
blog people, due to it’s good articles or reviews
Why visitors still use to read news papers when in this technological world all is available on net?
I have read so many posts on the topic of the blogger lovers except this article is truly a pleasant post, keep it up.
What’s up to all, the contents existing at this
website are actually awesome for people experience, well,
keep up the nice work fellows.
Hi there this is kind of of off topic but I was wondering
if blogs use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding experience so I wanted to get advice from someone with experience.
Any help would be greatly appreciated!
WOW just what I was looking for. Came here by searching for situs slot online indonesia
Very nice post. I just stumbled upon your blog and wished to say that I’ve
really enjoyed browsing your blog posts. After all I’ll be subscribing
to your feed and I hope you write again soon!
I’m impressed, I have to admit. Rarely do I come across a blog that’s both equally
educative and interesting, and without a doubt, you have hit
the nail on the head. The issue is something that not enough people are speaking intelligently
about. I’m very happy I found this during
my search for something relating to this.
I am in fact grateful to the owner of this web site who
has shared this great paragraph at at this place.
Hello, I would like to subscribe for this blog to get newest updates, so where can i do it please assist.
This is a topic that is near to my heart… Take care!
Exactly where are your contact details though?
Hi there! I could have sworn I’ve been to this web site before but after
browsing through a few of the articles I
realized it’s new to me. Regardless, I’m certainly delighted I came across it and I’ll be book-marking it
and checking back often!
Howdy! This is my first visit to your blog!
We are a group of volunteers and starting a new
project in a community in the same niche. Your blog provided us beneficial information to work on. You have done a wonderful job!
I simply could not go away your site prior to suggesting that
I extremely enjoyed the standard information a person provide
to your visitors? Is gonna be back regularly in order to
check out new posts
Thanks for ones marvelous posting! I really enjoyed
reading it, you might be a great author.I will remember to bookmark
your blog and definitely will come back later in life.
I want to encourage you to ultimately continue your great writing,
have a nice holiday weekend!
If you are going for finest contents like myself, just pay a
quick visit this web page daily for the reason that it gives quality contents, thanks
If you want to obtain a good deal from this piece of writing
then you have to apply such methods to your won web site.
I have been browsing online more than 4 hours today,
yet I never found any interesting article like yours. It is pretty worth enough for me.
Personally, if all webmasters and bloggers made good content as you did, the net will
be a lot more useful than ever before.
When I initially left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and now each
time a comment is added I recieve 4 emails with the exact same comment.
There has to be a means you can remove me from that service?
Thank you!
Hello, I enjoy reading through your post. I like to write
a little comment to support you.
I loved as much as you will receive carried out right here.
The sketch is tasteful, your authored material stylish. nonetheless, you command
get got an edginess over that you wish be delivering
the following. unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this hike.
It is truly a nice and useful piece of information. I’m
happy that you shared this helpful information with us. Please stay us up to date like this.
Thanks for sharing.
Hello, i think that i saw you visited my
website thus i came to “return the favor”.I am trying to find things to enhance my site!I suppose its ok to
use a few of your ideas!!
If you are going for finest contents like I
do, only pay a visit this web page daily as it provides quality contents, thanks
Hello to all, how is everything, I think every one is getting more from this web site, and your views are good in favor of new
users.
Hello there! Do you know if they make any plugins to protect
against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any
recommendations?
Great info. Lucky me I discovered your site by accident (stumbleupon).
I’ve book-marked it for later!
Hi, just wanted to say, I liked this blog post. It was practical.
Keep on posting!
Excellent post but I was wanting to know if you
could write a litte more on this topic? I’d be very grateful if
you could elaborate a little bit further. Thanks!