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
Da warten u.a. Saunen und einem Indoor- und Outdoorpool.
Außerdem wurde es bereits mehrfach zum besten Skigebiet der Welt gekürt!
Direkt bei den Liften des Skigebiets Obergurgl – Hochgurgl steht das Top
bewertete 4°S The Crystal mit seinem fantastischen 2.000
m² Spa-Bereich auf 2 Etagen. Die Gründe liegen klar auf der Hand: Neben den modernsten Aufstiegsanlagen und den vielen traditionellen Hütten zum Einkehren findet
ihr hier… Servus Piraten! Interessiert an einen luxuriösen Trip ganz ans
Ende des Ötztals? Dahinter stehen zwei bekannte Persönlichkeiten, als da sind Hermann Maier
& Rainer Schönfelder, welche mit dieser neuen Hotelmarke (ursprünglicher Name adeo) Skiurlaub für jedermann erschwinglich
machen, was ihnen mit ihren Hotelprojekten auf alle fälle gut… Mit
fast 300 Pistenkilometern und knapp 100 Liftanlagen gehört die SkiWelt Wilder Kaiser-Brixental zu den größten und modernsten Skigebieten Österreichs und der Welt.
Auch zum diesjährigen Saisonfinale Ende März bekommt ihr
noch Termine. Falls ihr diesjährig nicht zum Rennen dort wart, macht das nichts, denn Snowtrex hat bereits die Buchungstermine für Tag X Jahr drin, bei denen ihr ein gerüttelt maß sparen könnt.
Ahoi Piraten! Matthias Mayer heisst der diesjährige
Sieger des Hahnenkammrennens auf der Kitzbühler
“Streif”. Wem die 110 km Piste auf einer Höhe zwischen…
I’ll immediately seize your rss as I can not to find your e-mail
subscription link or newsletter service. Do you have any? Kindly permit me realize
so that I may subscribe. Thanks.
I do not even know how I ended up here, but I thought this
post was good. I do not know who you are but definitely you’re going to a famous blogger if you are not already 😉 Cheers!
I always used to study piece of writing in news papers but now as
I am a user of web therefore from now I am using net for articles,
thanks to web.
Hi, i feel that i saw you visited my weblog thus i came to return the
favor?.I am attempting to find issues to enhance my web
site!I assume its ok to use a few of your ideas!!
Hello, its pleasant article about media print,
we all be aware of media is a great source of data.
Hi it’s me, I am also visiting this web site on a regular basis, this web site is in fact fastidious and the
viewers are genuinely sharing fastidious thoughts.
Having read this I thought it was extremely enlightening.
I appreciate you spending some time and effort to put this information together.
I once again find myself personally spending way too much time both reading and leaving comments.
But so what, it was still worth it!
I love what you guys are usually up too. Such clever work and reporting!
Keep up the amazing works guys I’ve incorporated you guys to my blogroll.
I always spent my half an hour to read this blog’s articles or reviews all
the time along with a mug of coffee.
Fantastic items from you, man. I have be aware your stuff previous to and you’re simply too wonderful.
I actually like what you’ve received here, certainly like what you’re saying and the way by which you say
it. You are making it enjoyable and you still care for to keep it smart.
I cant wait to read much more from you. This is really a tremendous web
site.
Keep this going please, great job!
Hurrah, that’s what I was looking for, what a information! present here at this webpage, thanks admin of this
website.
Today, while I was at work, my sister stole my
iPad and tested to see if it can survive a 40 foot drop, just so she can be a youtube sensation. My iPad is now
broken and she has 83 views. I know this is totally off topic but I had
to share it with someone!
This excellent website definitely has all the info I needed about this subject
and didn’t know who to ask.
Thanks for the marvelous posting! I truly enjoyed reading it, you might be a great author.
I will remember to bookmark your blog and may come back from now on. I want to encourage you to continue your great work, have a nice morning!
Unquestionably believe that which you said. Your favorite reason appeared to be
on the net the simplest thing to be aware of.
I say to you, I certainly get annoyed while people consider worries that they just
don’t 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
Hi, i read your blog occasionally and i own a similar one and i
was just wondering if you get a lot of spam feedback? If so how do you prevent it,
any plugin or anything you can suggest? I get so much lately it’s driving me crazy so any support is
very much appreciated.
Pretty nice post. I just stumbled upon your weblog and
wanted to mention that I’ve truly enjoyed surfing around your weblog posts.
After all I’ll be subscribing on your feed and I’m
hoping you write once more soon!
Hello to all, how is everything, I think every one is getting more from this website, and your views are nice for new people.
Hey! This is my first visit to your blog! We are a group of
volunteers and starting a new project in a community in the same
niche. Your blog provided us beneficial information to work on. You have
done a marvellous job!
Hey there! Do you use Twitter? I’d like to follow
you if that would be ok. I’m absolutely enjoying your blog and look forward
to new updates.
I think the admin of this site is in fact working hard in support of his web page,
since here every information is quality based information.
I really like looking through an article that can make people think.
Also, thanks for permitting me to comment!
Excellent weblog right here! Also your website so much up very fast!
What host are you using? Can I am getting your associate link on your host?
I want my website loaded up as fast as yours lol.
We are a group of volunteers and opening a new
scheme in our community. Your web site provided us with valuable info
to work on. You have done a formidable job and our entire community will be thankful
to you.
Thank you for some other informative web
site. Where else may I am getting that kind of information written in such an ideal way?
I’ve a undertaking that I am just now running on, and I’ve been at the look out for such info.
hello!,I really like your writing very much! proportion we keep up a correspondence
extra approximately your article on AOL? I need a specialist in this house to
resolve my problem. Maybe that is you! Taking a look ahead
to peer you.
When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is
added I get four e-mails with the same comment. Is there any way you can remove me from that service?
Cheers!
I was pretty pleased to uncover this page. I need to to thank you for ones time
for this particularly wonderful read!! I definitely loved every
part of it and i also have you book-marked to check out new
information on your website.