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
where to buy generic finpecia pills
See What Railroad Injuries Lawsuit Tricks The Celebs
Are Utilizing railroad injuries lawsuit
Five Essential Qualities Customers Are Searching For In Every Stove 913875
Hello I am so happy I found your blog, I really found you by error, while I was
researching on Bing for something else, Nonetheless I am here now and would just like to say thanks for a remarkable post and a all round enjoyable blog (I also love the theme/design), I don’t have time to read it all at the minute 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 awesome b.
Beware Of These “Trends” About Best Coffee Machines http://www.4182051.xyz
The Top Reasons Why People Succeed In The Slots Industry kaymell.uk
can i order cleocin without dr prescription
як її перенести? Що відчувають при
всьому цьому рідні та близькі?
тоді вам – треба за цією адресою!
Also visit my blog post :: ibio.com.ua
What’s The Job Market For Online Shopping Uk For Clothes
Professionals Like? online shopping uk for clothes
The Reasons Act Fela Is More Difficult Than You Imagine lawyer
Hello there! comment-avatar great site.
can i purchase cheap glycomet without prescription
10 Quick Tips About Asbestos Exposure Attorney cassylawn.top
What’s The Current Job Market For Upvc Repairs Near Me Professionals Like?
Upvc Repairs Near Me
Asbestos may be exposed in many different ways. The majority of asbestos case (http://www.dinasboatyard.com)-related claims relate to occupational
exposure.
Быстрая регистрация и простая верификация в 1win казино
1win 1win .
Drugs information sheet. Long-Term Effects.
bactrim sale
Best about medicament. Get here.
The 9 Things Your Parents Taught You About Online Famous Shopping Sites online Famous shopping sites
Drugs information. Generic Name.
cialis soft generic
Everything news about drugs. Get now.
7 Things About Mesothelioma Law Firm You’ll Kick Yourself For Not Knowing mesothelioma Settlement – 50.Staikudrik.com,
Drug information for patients. Drug Class.
get generic trileptal prices
All trends of medicament. Get information here.
Играйте в любимые игры с мобильного устройства в 1win казино
1win официальный сайт 1win официальный сайт .
You can take Hunter Emphasis with other supplements.
My website :: review mushroom head air cushion Cc cream
5 Reasons To Consider Being An Online Carlocksmith And
5 Reasons To Not elsycrays.top
Nice blog! Is your theme custom made or did you download it from somewhere?
A theme like yours with a few simple adjustements would really make my blog shine.
Please let me know where you got your design. Thank you
Have a look at my site; is phenq safe to use
Hey there! This is kind of off topic but I need some help from an established blog.
Is it difficult to set up your own blog? I’m not very techincal but I can figure things out pretty fast.
I’m thinking about creating my own but I’m not sure where to start.
Do you have any points or suggestions? Thanks
The website https://search-groups.com/ likely offers a lot of useful information and life hacks because it targets a broad audience interested in practical and effective solutions for everyday tasks. These types of tips and tricks tend to attract attention and engagement.
I like the helpful information you provide in your articles.
I’ll bookmark your weblog and check again here regularly. I am quite certain I will
learn lots of new stuff right here! Best of luck for the next!
Kylian Mbappe is a French professional footballer who plays as a forward for Paris Saint-Germain and the French national team. Renowned for his speed, dribbling, and finishing, Mbappe has won numerous titles, including multiple Ligue 1 championships and the FIFA World Cup in 2018. Find out more about him here – https://kylianmbappe.paris-saint-germain-ar.com/