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
Hello there! I could have sworn I’ve visited this site before but after
looking at a few of the posts I realized it’s new to me.
Anyways, I’m certainly happy I stumbled upon it and
I’ll be bookmarking it and checking back often!
Wow! This blog looks exactly like my old one! It’s on a entirely different subject
but it has pretty much the same layout and design. Superb choice of colors!
Hello this is somewhat of off topic but I was wanting to know
if blogs use WYSIWYG editors or if you have to manually
code with HTML. I’m starting a blog soon but have no coding knowledge so I wanted to
get advice from someone with experience.
Any help would be greatly appreciated!
%%
Visit my web blog :: seat car keys – Lori,
How to file an asbestos claim [https://botdb.win]
It’s wonderful that yoou aare getting thoughts frdom this article aas
well as from our discusskon madee aat this place.
Thanks for snaring your thoughtts onn 5042. Regards
My brother suggested I would possibly like this web site.
He was entirely right. This put up truly made my day. You cann’t
consider simply how so much time I had spent for this information! Thank
you!
I quite like reading through an article that will make men and women think.
Also, thanks for permitting me to comment!
After exploring a number of the blog posts on your blog, I seriously
appreciate your way of writing a blog. I saved as a favorite it to my bookmark website list
and will be checking back in the near future. Take a
look at my web site as well and tell me what you
think.
Door Repair Richmond
It’s actually a great and helpful piece of information. I am happy that you shared this helpful
information with us. Please keep us up to date like this.
Thank you for sharing.
I every time used to read article in news papers but now as I am a user of
net therefore from now I am using net for articles, thanks to web.
It’s really a cool and helpful piece of info. I’m glad that you just shared this helpful information with us.
Please keep us informed like this. Thanks for sharing.
What’s uup colleagues, how is the whooe thing, annd what yyou want to sayy abot this puece oof writing, inn myy vjew itss really awesome
designedd for me.
Hi, I would like to subscribe for this website to take latest updates, so where
can i do it please assist.
%%
my web-site: Online Jobs Work From Home No Experience
asbestos claims (Archer)
Way cool! Some extremely valid points! I appreciate you penning this post and also the rest of the website is also really good.
%%
My blog post; double glazed windows lambeth (Jorja)
%%
Here is my site … diagnosis
Thanks in support of sharing such a pleasant opinion, article is
fastidious, thats why i have read it completely
%%
my homepage; legal Hash (gestiondachatsram.com)
how much asbestos can cause mesothelioma; grizzle-hyacinth-Fs3crq.mystrikingly.com,
%%
Here is my blog – private Mental health Care
Average Mesothelioma Lawsuit Settlements
Wow, amazing blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your site is wonderful, let alone the content!
asbestos lawsuit (Roxanna)
These are really impressive ideas in concerning blogging.
You have touched some fastidious points here. Any
way keep up wrinting.
Всем рекомендую!подготовка к международным экзаменам