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’m not that much of a online reader to be honest but
your blogs really nice, keep it up! I’ll go ahead and bookmark your site to
come back in the future. Cheers
My spouse and I absolutely love your blog and find a lot of your post’s to be precisely what I’m looking for.
Do you offer guest writers to write content for yourself?
I wouldn’t mind publishing a post or elaborating on a lot of the
subjects you write with regards to here. Again, awesome web site!
What’s up friends, how is all, and what you would like to say on the topic of this piece of writing, in my view its actually awesome in support of me.
This is very interesting, You’re a very skilled blogger.
I have joined your feed and look forward to seeking more of your excellent post.
Also, I’ve shared your site in my social
networks!
Woah! I’m really loving the template/theme of this blog.
It’s simple, yet effective. A lot of times it’s challenging to get that
“perfect balance” between user friendliness and visual
appeal. I must say that you’ve done a excellent job with
this. In addition, the blog loads super quick for me on Internet explorer.
Outstanding Blog!
Amazing blog! Do you have any tips and hints for aspiring writers?
I’m planning to start my own blog soon but I’m a little lost on everything.
Would you advise starting with a free platform like WordPress
or go for a paid option? There are so many choices out
there that I’m completely overwhelmed .. Any tips? Appreciate it!
Howdy! This is my first visit to your blog! We are a team of volunteers
and starting a new initiative in a community in the same niche.
Your blog provided us beneficial information to work on. You have done a
marvellous job!
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?
Neat blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple adjustements would really make my blog stand
out. Please let me know where you got your theme.
Appreciate it
Very good website you have here but I was curious if you knew of any discussion boards that
cover the same topics talked about here? I’d really like
to be a part of community where I can get suggestions from
other experienced individuals that share the same interest.
If you have any suggestions, please let me know. Kudos!
Awesome! Its genuinely remarkable post, I have got much clear idea about from this
article.
Very nice post. I just stumbled upon your blog and wished to say that I have
truly enjoyed surfing around your blog posts. After all I’ll be
subscribing to your rss feed and I hope you write again soon!
I am curious to find out what blog system you happen to be
using? I’m having some small security issues with
my latest website and I would like to find something
more secure. Do you have any solutions?
Hi there, just became alert to your blog through Google, and
found that it is truly informative. I am gonna watch out for
brussels. I will be grateful if you continue this in future.
Numerous people will be benefited from your writing.
Cheers!
You should take part in a contest for one of the greatest blogs online.
I am going to highly recommend this web site!
Excellent blog here! Also your web site loads up very fast!
What host are you using? Can I get your affiliate link
to your host? I wish my site loaded up as fast as yours
lol
What’s up, I would like to subscribe for this weblog to get hottest
updates, thus where can i do it please help.
Hello! This is kind of off topic but I need some advice from an established blog.
Is it very hard to set up your own blog? I’m not very techincal
but I can figure things out pretty quick. I’m thinking about creating my
own but I’m not sure where to start. Do you have any points or suggestions?
Appreciate it
Everything is very open with a clear description of the issues.
It was really informative. Your website is very useful.
Many thanks for sharing!
hello!,I love your writing very a lot! proportion we be
in contact extra approximately your post on AOL? I require a specialist on this area
to solve my problem. May be that is you! Looking forward to see you.
Hello! Someone in my Facebook group shared this site with us so I came to look
it over. I’m definitely enjoying the information. I’m
book-marking and will be tweeting this to my followers!
Excellent blog and superb design and style.
Amazing! This blog looks just like my old one! It’s on a entirely different topic but it has pretty much
the same page layout and design. Great choice of colors!
I constantly emailed this blog post page
to all my contacts, for the reason that if like to read it next my contacts will too.
Wonderful post! We will be linking to this great post on our website.
Keep up the great writing.
I’ve been surfing on-line more than 3 hours as of late, yet
I by no means found any attention-grabbing article like
yours. It is beautiful worth sufficient for me. In my opinion,
if all website owners and bloggers made excellent content as you did, the internet shall be
much more helpful than ever before.
bookmarked!!, I really like your blog!
This site was… how do you say it? Relevant!! Finally I’ve found something which helped me.
Appreciate it!
fantastic points altogether, you just received a new reader.
What could you recommend about your submit that you simply made some days
ago? Any certain?
Hey there just wanted to give you a brief heads
up and let you know a few of the pictures aren’t loading correctly.
I’m not sure why but I think its a linking issue.
I’ve tried it in two different internet browsers and both show the same results.
Yes! Finally something about demo slot joker.