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
5 Laws That Anyone Working In Designer Handbags And Purses Should Be Aware Of Designer Handbags manchester
http://viagras.online/# Generic Viagra for sale
See What Mazda Key Replacement Tricks The Celebs
Are Using mazda Key
Great post. I was checking constantly this explainer video blog and I am impressed! Very useful info specially the last part 🙂 I care for such info much. I was looking for this particular info for a long time. Thank you and best of luck. https://www.Starsandstripesfc.Com/users/explainer_video_company_india
http://kamagra.win/# Kamagra 100mg price
http://viagras.online/# Order Viagra 50 mg online
Excellent post. I was checking constantly this explainer video blog and I’m impressed! Very helpful information specially the last part 🙂 I care for such information much. I was looking for this particular info for a long time. Thank you and good luck. https://postherefree.com/536/posts/3-Services/27-Other/1976703-Explainer-Video-Company-India.html
How Much Can Online Shopping Figures Uk Experts Make?
4K Ultra Hd Ready Screen
Ten Marc Jacobs Tote Bagss That Really Improve Your Life marc jacobs
snapshot bag sale [Dorthea]
A Trip Back In Time The Conversations People Had About Drip Coffee Maker 20 Years Ago budget Coffee maker
The 10 Most Terrifying Things About Adhd In Older Females
Symptoms adhd in older females symptoms (Arnulfo)
Pretty nice post . I just stumbled upon your weblog and wanted to say that I have truly enjoyed browsing your blog posts. After all I will be subscribing to your rss feed and I hope you write again soon! https://pasteldirectory.Com/listings12704140/explainer-video-company-india
Watch Out: How Leeds Double Glazing Is Gaining Ground And What Can We
Do About It french door repairs leeds (Gretta)
How To Identify The Online Shopping Websites Clothes Right For You Durable
Metal Lens Hood [Elvira]
10 Tips To Know About Wall-Mounted Fireplace Fireplaces And Stove
Kamagra 100mg price kamagra oral jelly cheap kamagra
See What SEO Software Solution Tricks The Celebs Are Utilizing seo software solution (telegra.Ph)
Very nice post . I just stumbled upon your weblog and wished to say that I’ve truly enjoyed browsing your blog posts. In any case I?ll be subscribing to your feed and I hope you write again very soon! https://explainer57283.wikipublicist.com/4463873/explainer_video_company_india
11 “Faux Pas” You’re Actually Able To Create With Your Washers
And Dryers Combo washer dryer combos (nitka.by)
viagra canada Buy Viagra online cheap over the counter sildenafil
buy Kamagra Kamagra 100mg price cheap kamagra
https://kamagra.win/# Kamagra 100mg price
Everything is very open with a precise clarification of
the issues. It was definitely informative. Your site is very useful.
Thank you for sharing!
https://kamagra.win/# buy kamagra online usa
hello there and thank you for your information on explainer Videos ? I?ve definitely picked up something new from right here. I did however expertise some technical points using this site, as I experienced to reload the web site lots of times previous to I could get it to load properly. I had been wondering if your hosting is OK? Not that I’m complaining, but slow loading instances times will very frequently affect your placement in google and could damage your high quality score if advertising and marketing with Adwords. Anyway I am adding this RSS to my email and can look out for much more of your respective exciting content. Ensure that you update this again soon.. https://explainer57283.Wikipublicist.Com/4463873/explainer_video_company_india
Howdy, There’s no doubt that your blog could be having web browser compatibility issues.
When I look at your web site in Safari, it looks fine however when opening in I.E.,
it has some overlapping issues. I simply wanted to provide you with a quick heads up!
Besides that, wonderful website!
Buy Vardenafil online Buy generic Levitra online Levitra online pharmacy
It’s a shame you don’t have a donate button! I’d definitely donate to this superb blog!
I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account.
I look forward to fresh updates and will share this blog
with my Facebook group. Talk soon!
https://kamagra.win/# Kamagra Oral Jelly
It is explainer video company india perfect time to make some plans for the expaliner videos and it is time to be happy. I’ve read this post and if I could I wish to suggest you some interesting things or tips. Perhaps you could write next articles referring to this article. I want to read more things about it! https://www.techdirectory.io/professional-services/explainer-video-company-india