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 am extremely impressed with your writing skills and also with the layout on your blog.
Is this a paid theme or did you customize it yourself?
Anyway keep up the excellent quality writing, it is rare to
see a nice blog like this one these days.
I am truly happy to read this blog posts which consists of tons
of useful data, thanks for providing such information.
Good day! I just wish to give you a big thumbs
up for the great information you’ve got right here on this post.
I am coming back to your site for more soon.
I’ll immediately grasp your rss as I can’t in finding
your email subscription hyperlink or newsletter service. Do you’ve any?
Please allow me recognise so that I may just subscribe.
Thanks.
Hola! I’ve been following your blog for a long time now and
finally got the courage to go ahead and give you a shout out from Kingwood Tx!
Just wanted to say keep up the great work!
Very descriptive post, I liked that a lot. Will
there be a part 2?
What’s up i am kavin, its my first time to commenting anywhere, when i read this
article i thought i could also create comment due to this
good article.
Hello! 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?
Hello there! This post could not be written any better!
Reading this post reminds me of my good old room mate! He always kept
chatting about this. I will forward this post to him.
Fairly certain he will have a good read. Thanks for sharing! http://nana-variety.com
There are many resources with free psd files, just type in Google
and check them all before downloading because some of them may not be absolutely free.
I will provide you with a few links to get started:
create fake documents online Which is the best free software
download site?
It is better to use trustworthy sites for free download even if you have antivirus
installed on your system. There are many dangerous sites which
have malicious ads, fake download links, etc.
Thanks for the auspicious writeup. It in fact was a enjoyment account
it. Glance complex to more added agreeable from you!
However, how could we be in contact?
Wonderful website. Lots of useful info here.
I’m sending it to a few buddies ans also sharing in delicious.
And certainly, thank you on your sweat!
Hi there I am so glad I found your blog, I really found you
by error, while I was looking on Aol for something else, Anyhow
I am here now and would just like to say many thanks for a marvelous post and a all round entertaining
blog (I also love the theme/design), I don’t have time
to browse it all at the minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read more,
Please do keep up the great job.
I loved as much as you will receive carried out right here.
The sketch is tasteful, your authored subject matter stylish.
nonetheless, you command get bought an nervousness over
that you wish be delivering the following. unwell unquestionably come
further formerly again as exactly the same nearly a lot often inside case you shield this hike.
Thanks for your marvelous posting! I really
enjoyed reading it, you will be a great author.
I will always bookmark your blog and will eventually come back down the road.
I want to encourage yourself to continue your great writing, have
a nice evening!
I get pleasure from, lead to I found exactly what I was
taking a look for. You have ended my 4 day long hunt!
God Bless you man. Have a great day. Bye
I relish, cause I discovered just what I was taking a look for.
You’ve ended my 4 day lengthy hunt! God Bless
you man. Have a nice day. Bye
Hey There. I found your blog the usage of msn. That is an extremely well written article.
I will be sure to bookmark it and return to read extra of your helpful info.
Thanks for the post. I will certainly return.
Hey I know this is off topic but I was wondering if you knew of any widgets I could add to
my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would
have some experience with something like this. Please let me know if you run into anything.
I truly enjoy reading your blog and I look forward
to your new updates.
Wow, fantastic weblog structure! How lengthy have you been blogging for?
you make blogging glance easy. The overall look of your website is excellent,
let alone the content material!
It’s hard to come by educated people in this
particular topic, but you seem like you know what you’re talking
about! Thanks
Great – I should certainly pronounce, impressed with your web site.
I had no trouble navigating through all tabs and related info ended up being truly easy to do
to access. I recently found what I hoped for before you know
it in the least. Reasonably unusual. Is likely to appreciate
it for those who add forums or something, web site theme .
a tones way for your customer to communicate. Excellent task.
Hello! I know this is kind of off-topic however I needed to ask.
Does building a well-established website such as
yours take a lot of work? I’m brand new to writing a blog however I
do write in my journal everyday. I’d like to start a blog so I will be
able to share my own experience and views online.
Please let me know if you have any kind of suggestions or
tips for new aspiring bloggers. Appreciate it!
I am sure this paragraph has touched all the internet viewers, its
really really good post on building up new blog.
Do you have a spam issue on this website; I also am a blogger,
and I was wanting to know your situation; we
have developed some nice practices and we are looking to swap techniques with others, please shoot me an e-mail if interested.
Hi, i think that i saw you visited my weblog thus i came to return the want?.I’m attempting
to to find issues to improve my website!I guess its good enough to make use of some of your ideas!!
This design is steller! You definitely know how to keep a
reader amused. Between your wit and your videos, I was almost moved to
start my own blog (well, almost…HaHa!) Excellent job. I really enjoyed what you had to say,
and more than that, how you presented it. Too cool!
Hello, i think that i noticed you visited my website so i
came to return the desire?.I’m attempting to find things to improve my web site!I guess its
adequate to make use of some of your ideas!!
Hello, I log on to your blogs on a regular basis. Your humoristic style
is witty, keep up the good work!
Here is my web blog – Cash Offer Please
Hi, I do think this is a great web site. I stumbledupon it 😉 I will come back
once again since i have book marked it. Money and freedom is the greatest way to change, may you be rich and continue
to guide others.