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
Howdy! I know this is kind of off topic but I was wondering if you knew where I could
locate 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!
I’m not sure where you are getting your info, but great topic.
I needs to spend some time learning more or understanding more.
Thanks for fantastic info I was looking for this information for my
mission.
Hiya! Quick question that’s completely off topic. Do
you know how to make your site mobile friendly?
My web site looks weird when viewing from my iphone.
I’m trying to find a theme or plugin that might
be able to resolve this issue. If you have any suggestions, please share.
Appreciate it!
Hi to every , because I am truly keen of reading this website’s post to be updated daily.
It contains pleasant stuff.
Hello just wanted to give you a quick heads up
and let you know a few of the images aren’t loading properly.
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.
Wow, awesome weblog layout! How long have you been blogging for?
you made running a blog look easy. The whole glance of your site is great, as neatly as the content
material!
Undeniably believe that which you stated. Your favorite
reason appeared to be on the web the easiest thing to be aware of.
I say to you, I certainly get irked while people think about worries that they just do not know about.
You managed to hit the nail upon the top and defined out the
whole thing without having side effect , people could take a signal.
Will likely be back to get more. Thanks
What i don’t understood is in reality how you are not actually a
lot more well-liked than you might be right now. You’re so
intelligent. You understand thus considerably with regards to this subject, made me for my part believe it from so many varied angles.
Its like men and women aren’t fascinated unless it is something to do with Girl gaga!
Your own stuffs great. All the time care for it up!
Hi all, here every person is sharing these knowledge, so it’s nice to read
this website, and I used to pay a quick visit this webpage every day.
Heya! I understand this is somewhat off-topic however I had to ask.
Does operating a well-established website such as yours take
a massive amount work? I am brand new to operating a blog however I do write
in my journal daily. I’d like to start a blog so I can share my personal experience and
feelings online. Please let me know if you have any suggestions or tips for new aspiring bloggers.
Appreciate it!
My family every time say that I am wasting my time here at
web, but I know I am getting knowledge daily by
reading thes good posts.
Hello there! Do you use Twitter? I’d like to follow you if that would be okay.
I’m undoubtedly enjoying your blog and look forward
to new updates.
My brother recommended I might like this web site.
He was entirely right. This post actually made
my day. You can not imagine simply how much time I had
spent for this info! Thanks!
These are actually great ideas in regarding blogging.
You have touched some fastidious things here. Any way keep up wrinting.
I always spent my half an hour to read this web site’s articles
all the time along with a cup of coffee.
When I originally commented I clicked the “Notify me when new comments are added”
checkbox and now each time a comment is added I get several e-mails with the same comment.
Is there any way you can remove me from that service?
Cheers!
I every time spent my half an hour to read this blog’s posts daily along with a cup of coffee.
Wow, this article is pleasant, my sister is analyzing
such things, therefore I am going to inform her.
I loved as much as you’ll receive carried out right here.
The sketch is tasteful, your authored subject matter stylish.
nonetheless, you command get bought an shakiness over that you wish be
delivering the following. unwell unquestionably come more formerly
again since exactly the same nearly very often inside case you shield
this increase.
Thank you for every other fantastic post. Where else may anybody get
that kind of info in such a perfect manner of writing?
I have a presentation next week, and I am on the look for such information.
This paragraph offers clear idea in support of the new users of blogging,
that actually how to do running a blog.
Hey There. I found your blog using msn. This is an extremely
well written article. I’ll be sure to bookmark it and come back to read more of your useful info.
Thanks for the post. I will certainly comeback.
Hi there, You’ve done an excellent job. I’ll certainly digg it and personally recommend to my friends.
I am sure they will be benefited from this web site.
What’s up to every one, the contents existing at this web page
are really amazing for people knowledge, well, keep up the good work fellows.
After checking out a few of the blog articles on your website, I seriously appreciate your technique of blogging.
I book marked it to my bookmark webpage list and will be checking
back in the near future. Take a look at my website
as well and tell me how you feel.
What’s up friends, nice post and nice arguments commented at this place, I am truly enjoying by these.
Greetings from Los angeles! I’m bored to tears at work so I decided to check out your site on my iphone during lunch break.
I really like the info you present here and can’t wait to take a look when I get home.
I’m surprised at how quick your blog loaded on my phone ..
I’m not even using WIFI, just 3G .. Anyhow, amazing blog!
I for all time emailed this website post page to all my friends, as if like to read it then my friends will too.
Your means of telling all in this paragraph is in fact fastidious, all be capable of simply
understand it, Thanks a lot.
Hey there! I’ve been reading your web site for a while now and finally got the
courage to go ahead and give you a shout out from Houston Texas!
Just wanted to mention keep up the great job!