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
Fastidious response in return of this matter with real arguments and
describing everything regarding that.
Hi there! I could have sworn I’ve been to this site before but after reading through
some of the post I realized it’s new to me.
Anyhow, I’m definitely delighted I found it and
I’ll be book-marking and checking back often!
Thank you for the good writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you!
However, how can we communicate?
WOW just what I was searching for. Came here by searching for http://ezproxy.cityu.edu.hk/login?url=https://extrememining.org/
I know this if off topic but I’m looking into starting my own weblog and was curious what all
is needed to get set up? I’m assuming having a blog like yours
would cost a pretty penny? I’m not very internet savvy so I’m not 100% sure.
Any recommendations or advice would be greatly appreciated.
Thanks
My family always say that I am killing my time here at
web, except I know I am getting familiarity everyday by
reading thes nice content.
My partner and I stumbled over here different web page and thought I may as well check things out.
I like what I see so now i’m following you. Look forward
to looking into your web page for a second time.
It’s really very difficult in this busy life to listen news on Television, so I just use
the web for that reason, and get the most up-to-date information.
Good information. Lucky me I came across your blog by accident (stumbleupon).
I have saved it for later!
Hey just wanted to give you a quick heads
up. The text in your content seem to be running off the screen in Firefox.
I’m not sure if this is a formatting issue or something to do
with internet browser compatibility but I thought I’d post
to let you know. The style and design look great though! Hope you get the
problem fixed soon. Thanks
Way cool! Some very valid points! I appreciate you penning this article and the
rest of the website is really good.
Terrific article! That is the kind of info that are supposed to be shared across the internet.
Shame on the seek engines for no longer positioning this put up upper!
Come on over and seek advice from my site . Thanks =)
I’m gone to tell my little brother, that he should also
visit this webpage on regular basis to take updated from newest news update.
Wow, superb blog format! How lengthy have you been running a blog for?
you make running a blog look easy. The total look of your
site is magnificent, let alone the content!
What’s up to all, the contents existing at this site are actually
amazing for people experience, well, keep up the good work fellows.
I think the admin of this web site is genuinely working hard in favor of his web site, as here
every data is quality based material.
Hello! I just wanted to ask if you ever have any trouble with hackers?
My last blog (wordpress) was hacked and I ended up losing months
of hard work due to no back up. Do you have any solutions to protect
against hackers?
This article is in fact a fastidious one it helps new internet people,
who are wishing for blogging.
No matter if some one searches for his necessary thing, thus he/she needs to be
available that in detail, so that thing is maintained
over here.
Asking questions are actually nice thing if you are not understanding
something completely, except this post gives fastidious understanding even.
I read this paragraph completely concerning the resemblance of
hottest and previous technologies, it’s amazing article.
Please let me know if you’re looking for a writer for your site.
You have some really good posts and I feel I would be a good asset.
If you ever want to take some of the load off, I’d absolutely love to write some material for your blog in exchange for a
link back to mine. Please shoot me an email if interested.
Cheers!
Hello, i believe that i saw you visited my weblog thus i got here to go back the
choose?.I’m trying to in finding things to enhance my site!I suppose
its good enough to make use of some of your concepts!!
Hello, this weekend is pleasant designed for me, as this moment i am
reading this enormous educational paragraph here
at my residence.
I was very happy to discover this great site. I wanted to thank you for ones time for this particularly fantastic read!!
I definitely enjoyed every little bit of it and i also have you bookmarked to check out new information on your website.
Heya terrific blog! Does running a blog similar to this take a lot of work?
I have very little knowledge of computer programming but I had been hoping to start my own blog soon. Anyways,
should you have any suggestions or tips for new blog owners please
share. I understand this is off topic but I simply wanted to ask.
Appreciate it!
Your way of describing the whole thing in this paragraph
is really pleasant, all be capable of effortlessly know it, Thanks a lot.
Fastidious answer back in return of this issue with genuine arguments and telling everything regarding that.
I’m very pleased to uncover this website. I need to to
thank you for ones time for this fantastic read!!
I definitely liked every part of it and i also have you book-marked to look
at new things in your site.
Hi there, just wanted to tell you, I liked this
post. It was helpful. Keep on posting!