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
Your tips on spotting roof damage are spot on! Many people don’t realize how easy it is to miss issues until it’s too late tile repair
It’s interesting to learn about historical roofing styles shingle roofers
I’m planning to install solar panels residential roofing
The discussion around client-centric approaches in web design was insightful—get practical design tips via seo company for legal firms
If you’re looking for something eco-friendly, metal roofs are recyclable! Find out more benefits at roofers vancouver
Loved how you addressed balancing workloads amongst attorneys while ensuring each member feels valued contributing overall success towards growth initiatives moving forward through seo companies for lawyers
Wonderful tips on how to write effective blog posts as a lawyer! Content is king in legal SEO too! For additional advice, check out seo for legal firm
jack poker реальные отзывы
jack poker отзывы игроков
So glad I stumbled upon this blog post—it has given me many ideas regarding what kind of roof would suit my home best! metal roofers
Wow, this is eye-opening! I need to find a seo company for lawyers to take my firm’s online marketing to the next level
The versatility in design with modern metal roofing is remarkable—it fits so many architectural styles! Discover designs at roofing contractors vancouver
I do accept as true with all the ideas you have presented in your post.
They are really convincing and can certainly work.
Nonetheless, the posts are very short for starters. May you please
extend them a bit from next time? Thank you for the post.
buy amoxicillin online no prescription: cheap amoxil – amoxicillin 200 mg tablet
Effective communication seo companies for attorney
Way cool! Some very valid points! I appreciate you penning this article
and the rest of the website is really good.
Here is my homepage :: Pinoy SEO Services
Great job clarifying methods behind improving website load speed—a critical aspect affecting user experience directly impacting retention rates as discussed through seo for legal firm
When I initially commented I clicked the “Notify me when new comments are added” checkbox and now
each time a comment is added I get three emails with
the same comment. Is there any way you can remove people from that service?
Thanks!
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
darknet market dark market link https://darknetmarketstore.com/ – dark web link
Сколько стоит диплом высшего и среднего образования и как его получить?
Диплом вуза купить официально с упрощенным обучением в Москве
amoxicillin order online no prescription: amoxicillin 500mg capsules price – how to buy amoxicillin online
Быстрое обучение и получение диплома магистра – возможно ли это?
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
You are so interesting! I don’t think I’ve truly read through a single thing
like that before. So wonderful to discover somebody with a few
unique thoughts on this issue. Really.. thank you for starting this up.
This website is something that’s needed on the internet,
someone with a bit of originality!
Here is my web blog; Copper peptide serum
Good day I am so happy I found your blog, I really found you
by mistake, while I was browsing on Aol for something
else, Anyhow I am here now and would just like to say cheers for
a fantastic post and a all round interesting blog (I also love the
theme/design), I don’t have time to go through it
all at the moment but I have bookmarked it and also included your RSS feeds, so
when I have time I will be back to read more, Please
do keep up the superb job.
When someone writes an post he/she keeps the thought of a user in his/her brain that how a
user can be aware of it. So that’s why this post is outstdanding.
Thanks!
Hello fantastic blog! Does running a blog similar to this
take a lot of work? I’ve no knowledge of programming but I was hoping to start my
own blog in the near future. Anyways, should you have any recommendations or techniques for new
blog owners please share. I know this is off subject however I just
wanted to ask. Many thanks!
Check out my webpage personal injury lawyer phoenix az
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
amoxicillin 500 mg for sale: com pharm – amoxicillin 200 mg tablet