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
Howdy very nice site!! Guy .. Excellent .. Superb ..
I will bookmark your website and take the feeds also?
I am glad to seek out so many helpful info here
in the post, we need work out more techniques on this regard, thanks for sharing.
. . . . .
Great article, totally what I was looking for.
You can definitely see your expertise in the article you write.
The sector hopes for even more passionate writers such as you who aren’t afraid
to mention how they believe. All the time go after
your heart.
Hello! This is kind of off topic but I need some guidance from an established
blog. Is it hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast.
I’m thinking about setting up my own but I’m not sure where to begin. Do you have any tips or suggestions?
Appreciate it
What’s up colleagues, fastidious post and good urging commented here, I am truly enjoying by these.
Hi there mates, its wonderful paragraph concerning tutoringand completely explained, keep it up all the time.
I am really thankful to the holder of this web site
who has shared this enormous piece of writing at at this place.
Thanks , I’ve just been searching for information approximately this subject for a
long time and yours is the greatest I’ve came upon till
now. But, what about the bottom line? Are you certain in regards to the supply?
I loved as much as you will receive carried out right here.
The sketch is tasteful, your authored subject matter stylish.
nonetheless, you command get got an shakiness over that you wish
be delivering the following. unwell unquestionably come more formerly
again since exactly the same nearly a lot often inside case you shield this
hike.
I love your blog.. very nice colors & theme.
Did you design this website yourself or did you hire someone to
do it for you? Plz reply as I’m looking to construct my own blog and would like to find out where
u got this from. thanks a lot
Good day! I could have sworn I’ve been to this blog before but after browsing through some of the articles I
realized it’s new to me. Nonetheless, I’m certainly happy I stumbled upon it and I’ll be book-marking it and checking back
frequently!
Hi, just wanted to mention, I liked this
post. It was practical. Keep on posting!
What’s up mates, its wonderful article about
tutoringand fully defined, keep it up all the time.
Greetings from Colorado! I’m bored to death at work so I
decided to check out your site on my iphone
during lunch break. I love the information you present here and
can’t wait to take a look when I get home.
I’m shocked at how quick your blog loaded on my mobile ..
I’m not even using WIFI, just 3G .. Anyhow, good blog!
Hi my loved one! I wish to say that this article is
awesome, nice written and come with almost all significant infos.
I would like to see extra posts like this .
I’m curious to find out what blog system you’re working with?
I’m experiencing some small security issues
with my latest website and I’d like to find something more risk-free.
Do you have any suggestions?
Every weekend i used to pay a quick visit this web page,
for the reason that i want enjoyment, since this this website conations genuinely nice funny information too.
I’m not that much of a online reader to be honest but
your sites really nice, keep it up! I’ll go ahead and bookmark your website to come back down the
road. Many thanks
I’m very happy to uncover this site. I need to to thank
you for your time for this fantastic read!! I definitely savored every part of it and I
have you saved as a favorite to look at new information in your web
site.
Someone necessarily assist to make critically articles I would
state. That is the very first time I frequented your website page and to
this point? I amazed with the research you made to create this actual put up amazing.
Wonderful process!
I was suggested this website by my cousin. I am not
sure whether this post is written by him as no one else know such detailed about
my difficulty. You’re incredible! Thanks!
My brother suggested I might like this blog. He was entirely right.
This post truly made my day. You cann’t imagine simply
how much time I had spent for this information! Thanks!
Hi friends, good article and pleasant urging commented here, I
am truly enjoying by these.
I have been browsing online greater than 3 hours lately, yet I
never discovered any attention-grabbing article like yours.
It is lovely worth enough for me. Personally, if all webmasters and
bloggers made excellent content as you did, the web might be much more helpful than ever before.
Howdy are using WordPress for your blog platform?
I’m new to the blog world but I’m trying to get started and set up my own. Do you need any coding expertise to make your own blog?
Any help would be greatly appreciated!
Hello, everything is going perfectly here and ofcourse every one is sharing information, that’s in fact excellent, keep up writing.
Pretty! This has been an extremely wonderful article. Many
thanks for providing this information.
My developer is trying to convince me to move to .net from
PHP. I have always disliked the idea because of the expenses.
But he’s tryiong none the less. I’ve been using Movable-type on numerous websites for about a year
and am concerned about switching to another platform. I have heard great things about
blogengine.net. Is there a way I can transfer all my wordpress content into it?
Any help would be greatly appreciated!
It is really a nice and helpful piece of info. I am glad that
you simply shared this helpful info with us.
Please stay us informed like this. Thanks for sharing.
Hello to all, how is all, I think every one is getting more from this site, and your views are good for new people.