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
https://www.tomason-russia.ru – Узнайте больше о наших услугах на официальном сайте.
пин ап кз: пин ап кз – pinup kazi
Full Service Spa 유흥사이트
Have you ever thought about creating an e-book or guest authoring on other blogs?
I have a blog based upon on The Future of Online Casino
same topics you discuss and would really like to have you share some stories/information. I know my subscribers would enjoy
your work. If you’re even remotely interested, feel free to shoot me an e-mail.
Wonderful article! This is the kind of info that should
be shared across the web. Shame on the search engines for not positioning this submit upper!
Come on over and visit my site . Thank you =)
Do you mind if I quote a couple of your posts as long as I provide credit
and sources back to your website? My website is best realtor in Meridian Ms the very same niche as yours and my visitors would truly benefit from a lot of
the information you provide here. Please let me know if this ok with you.
Cheers!
Hi, i feel that i saw you visited my weblog so i came to return the want?.I am attempting
to in finding things to improve my web site!I guess its ok to make use
of a few of your ideas!!
Пошаговая инструкция по безопасной покупке диплома о высшем образовании
darknet drug market tor dark web darknet drugs
get gabapentin pill
Thank you for the good writeup. It in fact was
a amusement account it. Look advanced to far
added agreeable from you! By the way, how could we communicate?
Also visit my site – บาคาร่าออนไลน์
I was recommended this website by means of my cousin. I am now not positive whether
this put up is written by him as no one else recognise such distinctive approximately my trouble.
You’re amazing! Thank you!
Feel free to visit my web blog lottery tonight
Esim365 предоставляет практичный способ для связи за рубежом . Используя есим365 вы сможете подключиться к интернету в любой стране . Это решение идеально подходит для путешествий в Китай или Турцию .
Esim365 станет незаменимым помощником в поездках за границу . Вы можете использовать esim для Китая , где доступ к интернету может быть ограничен . Есим Турции обеспечит интернет в Турции .
Сервис есим 365 предоставляет удобный способ подключения к интернету за границей. Вы легко сможете настроить esim для путешествий . Вы всегда будете на связи, где бы ни находились .
вавада: вавада онлайн казино – vavada-kazi.ru
вавада казино онлайн вавада онлайн казино vavada-kazi.ru
Drugs information for patients. What side effects can this medication cause?
lisinopril 5 mg cost without insurance
Everything information about medicines. Read information here.
pinup: pinup-kazi.kz – pinup-kazi.kz
Body Massage OP
Hmm is anyone else encountering problems with
the pictures on this blog loading? I’m trying to figure out if its a problem
on my end or if it’s the blog. Any feedback would be greatly appreciated.
Как приобрести аттестат о среднем образовании в Москве и других городах
Thanks for sharing your thoughts on 1win. Regards
Esim365 предоставляет современное решение для связи за рубежом . Благодаря esim 365 вы получите доступ к интернету для заграницы . Это решение идеально подходит для путешествий в Китай или Турцию .
есим365 станет незаменимым помощником в поездках за границу . Удобно использовать есим для Китая , где доступ к интернету может быть ограничен . Также, с esim для Турции вы получите стабильную связь в Турции .
Сервис esim365 предоставляет удобный способ подключения к интернету за границей. Интернет для заграницы с esim365 подключается за считанные минуты. С таким решением интернет в Китае или Турции станет проще .
I used to be recommended this website by means of my cousin. I am now not sure whether this post is written via him
as no one else recognise such designated about my problem.
You’re wonderful! Thanks!
I found this very helpful. For additional info, visit personal injury lawyer
darknet websites darkmarket list tor market links
https://poppers51.ru
пин ап казино официальный сайт: пин ап казино – pinup kazi
вавада казино зеркало: вавада казино онлайн – vavada
I know this if off topic but I’m looking into starting my own blog and was wondering what all is required to get set up?
I’m assuming having a blog like yours would cost a pretty penny?
I’m not very internet savvy so I’m not 100% certain. Any suggestions or advice would be
greatly appreciated. Cheers
whoah this weblog is wonderful i like reading your posts.
Stay up the good work! You recognize, a lot of persons are looking round
for this information, you could aid them greatly.
Here is my web site: best realtor in st johns county fl