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
%%
Here is my web blog; mens moisturiser with spf
Hello there! I could have sworn I’ve been to this website before
but after reading through some of the post I realized it’s new to me.
Anyways, I’m definitely happy I found it and I’ll be bookmarking and checking back
often!
https://elpha.com/members/stocktradestarstock?tab=about
I hae bwen surfong online ore han tyree hhours today, yett I neve foundd anyy interesting
articfle lioke yours. It iis pretyty worrh enough forr me.
Personally, iff aall websige ownerss andd blggers mazde gokod
conttent ass yoou did, tthe weeb ill bee a llot moore seful tan ever before.
Alsoo visitt myy webb page; viking lotto arvonta
%%
Also visit my web-site … Spf moisturiser
%%
my site; night cream with retinol
payday loan
If some one needs expert view concerning running
a blog afterward i suggest him/her to pay a visit this website,
Keep up the good work.
Нi there, I enjoy reading ɑll of yօur article.
Ӏ like tto write a littlе comment tto support y᧐u.
Ⅿy blog … jasa backlink
Hmm it appears like your blog ate my first comment (it was super long) so I
guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog.
I as well am an aspiring blog blogger but I’m still new to everything.
Do you have any points for inexperienced blog writers?
I’d really appreciate it.
Heya i am for the first time here. I came across this board and I in finding It really useful & it helped me out much.
I hope to present something again and aid others like you aided me.
No matter if some one searches for his essential thing, therefore
he/she wishes to be available that in detail, so that thing is maintained over here.
Hello there! I know this is kinda off topic nevertheless I’d figured I’d ask.
Would you be interested in trading links or maybe guest writing a blog article
or vice-versa? My website addresses a lot of the same subjects
as yours and I feel we could greatly benefit from each other.
If you’re interested feel free to send me an email. I look forward to hearing from you!
Great blog by the way!
%%
my webpage best night cream uk
Piece of writing writing is also a fun, if you be familiar with after that you can write if not it is complicated to
write.
If this particular person is not right for you, do
not fall down useless. Younger Victor makes use of electricity to
reanimate his useless dog, Sparky, in the 2012 Tim Burton movie.
In Milan he became the pinnacle of Studio Patricia Urquiola design division, remaining on the position till
2010. In 2012 he co-based Dante – Items and Bads, a furnishings and house accessory brand, together
along with his accomplice, contemporary artist Aylin Langreuter.
Because nursing a crush is type of a weird flex. If a crush texts her, she
benches me, but in any other case, we’re good. It’s
higher to just ask somebody out when you’re getting good vibes;
in any other case, you would possibly miss your chance!
So take the L, as a result of Kondo-ing someone who’s flawed for you out of your life (and heart) will be
sure you could have room for the correct particular person once they present up.
Stop stalking her on Instagram and take this quiz! If you want to know whether or not it’s best to ask her out or move the heck on, that is the quiz for you.
No. The truth is, she asks if I would like any assist asking them out.
Hi, I do believe this is a great blog. I stumbledupon it 😉 I may come back yet
again since I book marked it. Money and freedom
is the best way to change, may you be rich and continue to guide other people.
%%
My web-site … night cream Best
%%
Here is my blog :: best Night cream
This text is priceless. Where can I find out more?
What a information of un-ambiguity and preserveness of valuable familiarity concerning unexpected feelings.
Hey There. I found your blog using msn. This is an extremely
well written article. I will make sure to bookmark
it and return to read more of your useful information. Thanks for the post.
I will definitely return.
%%
Feel free to visit my web blog; spf30 Moisturiser
I’ve been surfing online greater than three hours today, yet I
never found any interesting article like yours.
It’s pretty worth sufficient for me. Personally, if all web owners and bloggers made excellent content material as you probably
did, the internet might be a lot more useful than ever before.
monapinavir molnupiravir 200
https://vimeo.com/user183762571
Appreciate the recommendation. Let me try it out.
Hello just wanted to give you a brief heads up and let you know a few of the images
aren’t loading properly. I’m not sure why but I think its a linking
issue. I’ve tried it in two different browsers and
both show the same outcome.
Lovely just what I was looking for. Thanks to the author for taking his clock time on this one.
Here is my web-site :: Rescue Hair 911 Ingredients
Generally I do not read article on blogs, but I would like to say that this write-up very compelled me to check out and do it!
Your writing taste has been surprised me. Thank you,
quite great post.