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
How To Make A Successful Repairing Double Glazed Windows Instructions For Homeschoolers From Home
Double glazed window repairs near me (qooh.me)
10 Kia Key Fob Replacement Techniques All Experts Recommend 2015 kia sedona key fob programming (Oliver)
Заказать бурение на воду можно на сайте мастер-буров.рф уже сейчас. Скважина с чистой водой — один из важных компонентов на Вашем участке. Мы даем гарантию на качество проделанной работы, материалов, а также осуществляем сервисное обслуживание. Природные факторы могут разрушительно влиять на скважину и качество воды, мы также работаем с восстановлением поврежденных скважин от деформации и с очищением от заиливания. Звоните, приходите, будем рады с Вами сотрудничать.
20 Tips To Help You Be More Efficient At Slot Experiences 3D Slots
I am extremely impressed with your writing abilities and also with the layout in your blog.
Is this a paid topic or did you modify it yourself? Anyway stay up
the nice high quality writing, it is uncommon to look a
great weblog like this one nowadays..
Feel free to visit my blog post: lung clear pro
You’ll Never Guess This Fela Lawsuit Settlements’s Tricks Fela Lawsuit settlements
A Double Glazed Window Repairs Near Me Success Story You’ll Never Believe double glazing repair Near me
Five People You Must Know In The Akun Demo Slot
Industry Demo Slot Heylink
What’s The Job Market For Slot Software Professionals?
slot Software (http://tkinterdesigner.com/)
This design is wicked! You certainly know how to keep a reader entertained.
Between your wit and your videos, I was almost moved to start my own blog
(well, almost…HaHa!) Excellent job. I really enjoyed what you
had to say, and more than that, how you presented it.
Too cool!
15 Of The Best Pinterest Boards All Time About Online Shopping 2-inch receiver ball mount
20 Fun Facts About Best Slot Payouts exciting casino slots
Hi there! Quick question that’s completely off topic. Do you know how to make your site mobile friendly?
My site looks weird when viewing from my iphone. I’m
trying to find a template or plugin that might be able to correct this issue.
If you have any suggestions, please share. Many thanks!
Каждый год в середине сентября проводится Тюменский инновационный форум «НЕФТЬГАЗТЭК».
Форум посвящен развитию механизмов инновационного роста секторов топливно-энергетического комплекса, обсуждению и изысканию решений, организации благоприятных обстоятельств для расчета инновационных проектов. Ежегодный тюменский форум является влиятельной дискуссионной площадкой по продвижению нефтегазовой отрасли в Российской Федерации, имеет высокий авторитет и актуальность, созвучен общей стратегии формирования инноваторского курса в Российской Федерации
-https://neftgaztek.ru/
The 10 Most Terrifying Things About Mobile Slots mobile Slots –
xypid.win,
15 Things You Didn’t Know About Window Repair Near upvc window Repair Near me
What Is Window Sash Repairs And How To Use What
Is Window Sash Repairs And How To Use window Repair
9 Things Your Parents Teach You About Online Shopping Websites For Clothes Online Shopping Websites For Clothes (http://Jakochem.Com/Bbs/Board.Php?Bo_Table=Free&Wr_Id=206908)
Everything You Need To Know About Bentley Bentayga Key bentley key cover
What’s Holding Back From The Slot Challenges
Industry? Slots With Free Spins (https://Zzb.Bz)
Why You Must Experience Low Limit Slots At Least Once In Your Lifetime
top jackpot Slots
Nous assurons l’installation, le curage
et le nettoyage des canalisations. Notre équipe professionnelle se tient à votre disposition pour tous vos besoins.
https://logopond.com/velicio/profile/683207/?filter=&page=
I think this is among the most vital information for me.
And i am glad reading your article. But should remark on some general things, The web site style is perfect, the articles is
really great : D. Good job, cheers
20 Up-And-Comers To Follow In The Treadmill Foldable Incline Industry folding treadmill with incline
https://tituszukz73838.prublogger.com/27249782/pin-up-casino-and-mostbet-your-gateway-to-thrilling-enjoyment-and-profitable-betting
The One Shopping Online Uk To Ireland Mistake Every Beginner Makes electric welding strapping Tool
https://jasperzpdr49494.ageeksblog.com/27105182/pin-up-casino-and-mostbet-your-gateway-to-thrilling-entertainment-and-worthwhile-betting
The 9 Things Your Parents Taught You About Affordable SEO Agency Affordable backlink
20 Trailblazers Setting The Standard In Slot Games
Classic Slots