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
Thank you for every other wonderful article. The place else could anybody get that type of information in such a perfect
way of writing? I have a presentation next week, and I’m on the look for
such information.
I think the admin of this web page is genuinely working hard for
his site, for the reason that here every data is quality based data.
Have you ever considered about including a little bit
more than just your articles? I mean, what you say is fundamental
and all. Nevertheless just imagine if you added some great images or videos to
give your posts more, “pop”! Your content is excellent but with pics and video clips, this blog could undeniably be one of the greatest in its niche.
Good blog!
Aufm Drahtesel durch das magische Wattenland?
In dem Stil lässt sich das wunderschöne Gebiet ausgezeichnet erkunden. Goldgelbe Äcker, grüne Schorren, mäandernde Wasserläufe, die Silbertöne des trockengefallenen Watts, die weißen Wolkenhimmel – und alle vier
Kilometer ein malerisches Dorf. Zusammengefasst:
Eine Radtour durch Wattenland ist eine Augenweide.
Es herrscht wirklich genügend Gelegenheiten, unterwegs Hand in Hand gehen der zahlreichen gemütlichen Restaurants, Lokale oder Ruheoasen eine kleine Verschnaufpause einzulegen. Hier weht
der Wind wahrscheinlich immer etwas kräftiger,
aber das gehört einfach dazu. Über das umfangreiche Radwandernetz mit zahlreichen Knotenpunkten können Sie sich ausgezeichnet auf den hunderte Kilometer
langen komfortablen Radwegen orientieren. Anhand der grünweißen Knotenpunktschilder finden Sie dann Ihren Weg.
Herrlich durchs Groninger Land radeln, die fantastische Landschaft bewundern, die schier unendliche
Weite genießen, unterwegs picknicken und als
Sahnehäubchen eine schmackhafte biologische Mahlzeit.
Ein einzigartiges Konzept, in dessen Mittelpunkt die
Natur mit allem, was sie zu bieten hat, steht.
Your way of explaining everything in this paragraph is truly nice, all
can without difficulty know it, Thanks a lot.
For latest news you have to pay a quick visit the web and on internet I found this site
as a finest web page for most recent updates.
Thank you for sharing your info. I truly appreciate your efforts and I am waiting for your next post thank you once again.
you are truly a excellent webmaster. The web site loading pace is incredible.
It seems that you are doing any unique trick. Moreover,
The contents are masterpiece. you have done a great task on this topic!
Normally I do not read post on blogs, but I wish to say that this write-up
very compelled me to check out and do so! Your writing style
has been amazed me. Thanks, quite great post.
What a information of un-ambiguity and preserveness of valuable know-how concerning unexpected emotions.
Hey there! I know this is kinda off topic however , I’d figured I’d ask.
Would you be interested in trading links or maybe guest writing
a blog article or vice-versa? My site addresses a lot of the
same topics as yours and I believe we could greatly benefit from each other.
If you’re interested feel free to send me an e-mail. I look forward to hearing from you!
Superb blog by the way!
When I originally left a comment I appear to have clicked
the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I get four emails with the exact same comment.
Is there a means you are able to remove me from that service?
Many thanks!
This is a topic which is near to my heart… Cheers! Where are your contact
details though?
Whats up very cool site!! Guy .. Beautiful .. Amazing ..
I will bookmark your blog and take the feeds additionally?
I’m glad to find a lot of helpful information here in the publish, we want work out more
strategies in this regard, thanks for sharing.
. . . . .
Thanks to my father who told me on the topic of this website,
this web site is truly awesome.
Hello there! This post couldn’t be written much better!
Looking through this post reminds me of my previous roommate!
He always kept preaching about this. I will forward this post to him.
Pretty sure he’s going to have a great read. Many thanks for sharing!
Hmm is anyone else having problems with the images on this blog loading?
I’m trying to find out if its a problem on my end or if it’s the blog.
Any responses would be greatly appreciated.
Do you mind if I quote a few of your articles as long as I provide credit and sources back to your site?
My website is in the exact same area of interest
as yours and my visitors would genuinely benefit from some of the
information you present here. Please let me know
if this alright with you. Regards!
Hi would you mind stating which blog platform you’re using?
I’m going to start my own blog in the near future but I’m having a hard time making
a decision between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique.
P.S Sorry for getting off-topic but I had to ask!
Looking for SEO services? Or need consultancy
in SEO or digital Marketing? Contact khurram Jamil,
He is Google Certified SEO Expert and Trainer. you can reach
him at https://web.facebook.com/khurramjamilseo/
Hello! Do you use Twitter? I’d like to follow you if that would be ok.
I’m definitely enjoying your blog and look forward to new updates.
Very good blog you have here but I was wanting to know if you knew
of any community forums that cover the same topics talked about in this article?
I’d really like to be a part of group where I can get suggestions from other experienced people that share the same
interest. If you have any suggestions, please let me know.
Many thanks!
I just could not leave your website prior to suggesting that I really
loved the standard information an individual provide to your visitors?
Is gonna be again steadily in order to check out new posts
Hi there every one, here every person is sharing these experience, thus it’s good to read
this weblog, and I used to visit this blog everyday.
We’re a group of volunteers and starting a new scheme in our community.
Your website provided us with valuable information to work on. You’ve
done a formidable job and our whole community will be thankful
to you.
Hello friends, good article and nice urging commented here,
I am truly enjoying by these.
I used to be suggested this blog by way of my cousin. I’m no longer sure
whether or not this post is written by way of him as no one else realize such particular about my trouble.
You’re incredible! Thanks!
Hi there, just became aware of your blog through Google, and found that it is really informative.
I am going to watch out for brussels. I will be grateful if you continue this in future.
Lots of people will be benefited from your writing.
Cheers!
Spot on with this write-up, I actually feel this site
needs much more attention. I’ll probably be returning to
read through more, thanks for the info!
hello there and thank you for your info – I’ve certainly picked up something new from right
here. I did however expertise a few technical points using this web site, as I experienced to reload the web site
lots of times previous to I could get it to load correctly.
I had been wondering if your web host is OK? Not that I’m complaining, but sluggish loading instances times will sometimes affect your placement in google
and can damage your quality score if advertising and marketing with
Adwords. Well I am adding this RSS to my e-mail and can look out for a lot
more of your respective interesting content. Ensure that you update this again soon.