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
Link exchange is nothing else but it is just placing the
other person’s web site link on your page at appropriate place and other person will also do
same for you.
cipro for sale ciprofloxacin 500mg buy online where can i buy cipro online
https://ciprofloxacin.tech/# cipro 500mg best prices
I’m not that much of a online reader to be honest but your sites really nice, keep
it up! I’ll go ahead and bookmark your site to come back in the future.
Many thanks
my site … Poway Blonde Specialist
I am truly pleased to read this blog posts which consists of lots of useful data, thanks for providing these data.
Для каждого слота начисляют по a hundred фриспинов, то есть суммарно игрок может получить one thousand бесплатных вращений.
tamoxifen and osteoporosis: tamoxifen pill – femara vs tamoxifen
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 e-mails with the same comment.
Is there any way you can remove me from that service?
Appreciate it!
I read this post completely concerning the difference of most up-to-date and preceding
technologies, it’s awesome article.
Also visit my blog post … looking for sex near me
An Easy-To-Follow Guide To Mesothelioma Lawyer asbestos case
Everyone loves what you guys are up too. This kind of clever work and exposure!
Keep up the terrific works guys I’ve added you guys to my own blogroll.
Feel free to visit my homepage: 해외선물 대여업체
http://nolvadex.life/# nolvadex 10mg
win — букмекерская контора 1вин. Нынешняя вариация сайтов для ставок и игры на реальные деньги поражает. Для российских игроков открыты десятки ресурсов 1win
It’s truly a great and helpful piece of info. I am happy that you just shared this helpful info with us.
Please stay us informed like this. Thank you for sharing.
Take a look at my blog MLB중계
cipro pharmacy buy cipro ciprofloxacin 500 mg tablet price
lisinopril 20 mg daily: lisinopril 10 mg online no prescription – where to buy lisinopril
http://finasteride.store/# order generic propecia price
purchase cipro: buy ciprofloxacin over the counter – ciprofloxacin generic price
buy cytotec pills Cytotec 200mcg price buy cytotec
I stumbled upon your record while browsing as a service to interrelated dirt on the internet. It’s a dignity post! Please continue to share out and heed the message updated. https://sites.google.com/view/scaryhorrorgames/
order lisinopril online us lisinopril pill zestril 20 mg tab
https://ciprofloxacin.tech/# cipro
purchase lisinopril: lisinopril 10 mg online no prescription – lisinopril price uk
Hello, i think that i saw you visited my site thus i came to “return the favor”.I am attempting to find things to enhance my website!I
suppose its ok to use some of your ideas!!
buy cytotec online: cytotec abortion pill – Cytotec 200mcg price
http://cytotec.club/# cytotec buy online usa
I read this piece of writing fully on the topic of the comparison of most recent and preceding technologies, it’s remarkable article.
Appreciate this post. Will try it out.
cost of generic propecia online buying cheap propecia without a prescription buy propecia online