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 later on. All the best
My partner and I stumbled over here from a different page and thought
I may as well check things out. I like what I see so now i
am following you. Look forward to looking over your web
page for a second time.
Remarkable issues here. I am very satisfied to see
your post. Thank you so much and I’m having a look ahead to touch you.
Will you please drop me a e-mail?
Hello, i think that i noticed you visited
my blog thus i got here to return the choose?.I am trying to find things to improve my web site!I guess its ok to make
use of some of your ideas!!
Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your website?
My blog is in the exact same area of interest as yours and
my users would really benefit from some of the information you present here.
Please let me know if this alright with you. Cheers!
What’s up to all, the contents present at this site are in fact awesome
for people knowledge, well, keep up the good work fellows.
Eine solche Frisur passt perfekt zu den beiden Typen. Wavy Bob.
Weitestgehend sind Bob -Frisuren bis zum Kinn geschnitten,
können aber auch schulterlang gestylt werden, getreu dem eigenen Geschmack.
Ein angesagtes Beispiel für eher lockige, gewellte Haare ist der s.g.
Kurze Stufen bei Wavy Bob für lockige Haare
sind aber nix da empfehlenswert, da das Haar extrem kurz aussieht, wenn dieses gewellt im Einzelnen schon auf
Stufen geschnitten ist. Asymetrischer Bob liegt seit Kurzem unter den Damen besonders sexy.
Lassen Sie außerdem den Fakt nicht außer Acht, dass Bob -Frisuren eher verspielt und locker aussehen sollten, und nicht immer perfekt fixiert.
Die unterschiedlich geschnittenen Seiten dieser Frisur passen sowohl zu
glatten, als auch zu gewellten bis lockigen Haaren. Als
eine ausgefallene Ergänzung zum Bob wird der Kupferglanz in den Haaren betrachtet.
Dieser verwandelt das Haar in einer glamourösen, modernen Frisur,
deren man partout Aufmerksamkeit schenken würde!
Great post. I used to be checking constantly this weblog and I am inspired!
Very helpful info specifically the remaining section 🙂 I deal with such information a lot.
I was looking for this particular info for a very long time.
Thanks and good luck.
What’s up, this weekend is good in support of me,
as this moment i am reading this wonderful educational piece of
writing here at my residence.
Hi! I know this is somewhat off topic but I was wondering if you knew where I
could get 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!
Pretty! This was an incredibly wonderful post.
Thanks for supplying this information.
Undeniably believe that that you stated. Your favourite reason seemed to be on the
web the easiest factor to remember of. I say to you, I certainly get irked even as
other folks think about worries that they plainly don’t know about.
You controlled to hit the nail upon the top and also outlined out the
whole thing without having side-effects , folks can take a signal.
Will likely be back to get more. Thank you
Hello my friend! I want to say that this article is
awesome, great written and include approximately all important infos.
I’d like to see more posts like this .
Hey just wanted to give you a quick heads up. The
text in your post seem to be running off the screen in Safari.
I’m not sure if this is a format issue or something to do with internet browser compatibility
but I figured I’d post to let you know. The
design and style look great though! Hope you get the problem resolved
soon. Cheers
Great article, just what I needed.
This website certainly has all of the info I needed about this subject and didn’t know who to ask.
Aw, this was a really good post. Taking a few minutes and actual effort to make a really good article… but what can I say… I hesitate a lot and don’t manage to get anything done.
Howdy! This blog post couldn’t be written any better! Reading through this article reminds me of my previous roommate!
He continually kept talking about this. I most certainly will forward this post to him.
Pretty sure he’s going to have a good read. Thank
you for sharing!
Greetings from Los angeles! I’m bored to tears at work so I decided to browse your site on my iphone during lunch break.
I enjoy the information you provide here and can’t wait to take a look when I get home.
I’m shocked at how fast your blog loaded on my cell phone ..
I’m not even using WIFI, just 3G .. Anyhow, superb site!
Thanks for the marvelous posting! I genuinely enjoyed reading it, you may be a great author.
I will be sure to bookmark your blog and may come back someday.
I want to encourage that you continue your great posts, have a nice evening!
I was recommended this blog by my cousin. I’m not sure whether this post is written by him
as nobody else know such detailed about my trouble.
You’re amazing! Thanks!
Greetings from Idaho! I’m bored at work so I decided to check out your
blog on my iphone during lunch break. I enjoy the information you provide here and can’t wait to
take a look when I get home. I’m shocked at how fast your blog loaded on my mobile
.. I’m not even using WIFI, just 3G .. Anyhow, great blog!
I’ve been surfing on-line greater than three hours today, yet I
never found any attention-grabbing article like yours.
It’s beautiful worth sufficient for me. In my opinion, if all webmasters and bloggers made excellent content material as you probably
did, the internet can be much more helpful than ever before.
Hello there, just became aware of your blog
through Google, and found that it is truly informative.
I’m gonna watch out for brussels. I’ll be grateful if you continue
this in future. Lots of people will be benefited from your writing.
Cheers!
This blog was… how do you say it? Relevant!!
Finally I’ve found something that helped me. Kudos!
I really like it when individuals get together and share opinions.
Great blog, keep it up!
I am sure this paragraph has touched all the internet viewers, its really really fastidious
paragraph on building up new website.
Hi! Quick question that’s completely off topic. Do you know how to make your site mobile friendly?
My web site looks weird when browsing from my iphone 4.
I’m trying to find a template or plugin that might be able to correct this issue.
If you have any recommendations, please share. With thanks!
If you want to take a good deal from this post then you have
to apply these methods to your won weblog.
Hi there to all, it’s in fact a fastidious for me to pay a quick visit this web site, it contains priceless Information.