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
My relatives every time say that I am killing my time
here at net, but I know I am getting knowledge every day by reading such nice content.
Howdy! I understand this is sort of off-topic however I had
to ask. Does operating a well-established website such as yours take a lot of work?
I’m completely new to writing a blog however I do write in my journal daily.
I’d like to start a blog so I will be able to share my personal experience and feelings online.
Please let me know if you have any recommendations or tips for brand new aspiring blog
owners. Thankyou!
I think everything said made a lot of sense. But, consider
this, suppose you were to create a awesome headline?
I mean, I don’t wish to tell you how to run your website, but what if you added something to possibly grab folk’s attention? I mean JS 依照元素出現數量排序 –
馬老師 雲端研究室 is kinda vanilla.
You should peek at Yahoo’s front page and note how they create article
titles to get people to open the links. You might add
a related video or a related pic or two to grab readers interested
about what you’ve written. Just my opinion, it
could make your posts a little livelier.
Thank you for sharing your info. I really appreciate your efforts and I will be
waiting for your next post thank you once again.
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 later on. Cheers
We are a group of volunteers and starting a brand new scheme in our community.
Your website offered us with helpful info to work on. You have performed
a formidable job and our whole neighborhood can be grateful to you.
I simply could not depart your website before suggesting that I really
enjoyed the usual information a person supply for your
guests? Is going to be back frequently to inspect new posts
Howdy, i read your blog from time to time and
i own a similar one and i was just wondering if you get a lot of spam responses?
If so how do you stop it, any plugin or anything you can recommend?
I get so much lately it’s driving me crazy so any support
is very much appreciated.
Thanks to my father who shared with me concerning this website, this website is truly amazing.
My partner and I stumbled over here by a different web page and thought
I should check things out. I like what I see so i am just
following you. Look forward to finding out about your web page again.
What’s up friends, how is everything, and what you wish for
to say on the topic of this post, in my view its in fact amazing in support of me.
Pretty great post. I simply stumbled upon your weblog and wanted to mention that I have
really enjoyed browsing your blog posts. In any case I’ll be subscribing on your rss feed and I’m hoping
you write again soon!
I think the admin of this website is really working
hard for his web site, because here every material is quality based data.
Hi there, after reading this amazing article i am
as well cheerful to share my experience here with
colleagues.
This is the perfect web site for anybody who really wants to find out about this topic.
You understand a whole lot its almost tough to argue with you (not that I personally would want
to…HaHa). You definitely put a brand new spin on a subject that’s been discussed
for years. Great stuff, just great!
naturally like your web site however you have to check the spelling on quite a few
of your posts. Several of them are rife with spelling issues and I
in finding it very bothersome to tell the reality however I
will definitely come back again.
Its like you read my mind! You seem to know so much about this, like you wrote the book
in it or something. I think that you can do with a few pics
to drive the message home a little bit, but instead of that, this is magnificent blog.
A fantastic read. I will definitely be back.
This is a very good tip especially to those fresh to
the blogosphere. Simple but very precise information… Appreciate
your sharing this one. A must read post!
Hi just wanted to give you a brief 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 outcome.
Currently it sounds like Drupal is the best blogging platform available right now.
(from what I’ve read) Is that what you’re using on your blog?
I do not even know how I ended up here, but I thought
this post was great. I don’t know who you are but definitely you
are going to a famous blogger if you aren’t already 😉 Cheers!
Great information. Lucky me I discovered your website by chance (stumbleupon).
I’ve saved it for later!
Hi there! I know this is kinda 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 difficulty finding one?
Thanks a lot!
Hello my family member! I wish to say that this article is awesome, nice written and come with almost all significant infos.
I would like to peer extra posts like this .
Greetings! Very helpful advice within this article!
It is the little changes that will make the most important changes.
Many thanks for sharing!
After looking over a handful of the articles on your web page, I truly appreciate your technique
of writing a blog. I saved it to my bookmark webpage list and will be checking back in the near future.
Please visit my website too and tell me what you think.
We’re a group of volunteers and starting a brand new scheme in our community.
Your site offered us with useful info to work on. You’ve performed an impressive process and our whole group
might be thankful to you.
Hello there! Do you know if they make any plugins to safeguard against hackers?
I’m kinda paranoid about losing everything I’ve worked hard on. Any
tips?
Hi, i read your blog from time to time and i own a similar one and i was just
curious if you get a lot of spam remarks? If so how do you stop it, any
plugin or anything you can suggest? I get so much lately
it’s driving me mad so any assistance is very much appreciated.
Thanks for sharing your info. I truly appreciate your efforts
and I am waiting for your next post thank you once again.