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
7 Little Changes That’ll Make A Big Difference With Your Auto Accident Attorney Auto Accident Lawyer
The 12 Most Popular Leeds Door And Window Accounts To Follow On Twitter leeds glazing
The 3 Greatest Moments In Slot Updates History Low variance slots; https://www.highclassps.com:14015/bbs/board.php?bo_table=free&wr_id=1095330,
What Can A Weekly Double Glazing Glass Replacement Near Me Project Can Change Your Life double glazed windows repairs
Hiya! I know this is kinda off topic but I’d figured I’d ask.
Would you be interested in exchanging links or maybe guest writing a blog article or vice-versa?
My blog goes over a lot of the same subjects as yours and I believe we could greatly benefit from each other.
If you happen to be interested feel free to send me an email.
I look forward to hearing from you! Wonderful blog by the way!
The Reasons To Work With This Mesothelioma Legal Question mesothelioma compensation
5 Killer Quora Answers On Shark I Robot Vacuum vacuums
A Look Inside The Secrets Of Avon Uk Login Rep avon uk login rep – fpcom.co.kr,
Many Of The Common Errors People Do With Adult Test For ADHD Adhd Testing For Adults
You’ll Never Guess This Adult Adhd Assessment’s Secrets adhd assessment (http://lineyka.org/)
The Advanced Guide To Private Psychiatrist Ipswich psychiatrist private london
It’s in point of fact a great and helpful piece
of information. I am glad that you just shared this useful info with us.
Please stay us up to date like this. Thanks for sharing.
Comprehensive List Of Double Glazing Windows Leeds Dos And Don’ts glass replacement leeds – keating-korsholm-30.hubstack.net,
Think You’re Perfect For Locksmith Cars Near Me? Take This Quiz locksmith reprogram car key
(Tabatha)
The One Erb’s Palsy Lawyers Mistake Every Beginning Erb’s Palsy Lawyers User Makes erb’s Palsy lawsuits
The 10 Most Terrifying Things About Pvc Window
Repairs Window Repairs, http://0553721256.Ussoft.Kr/G5-5.0.13/Bbs/Board.Php?Bo_Table=Board01&Wr_Id=478360,
The 10 Most Scariest Things About Double Glazing Near Me Double glazing Near me
How To Recognize The Right Designer Handbags Sale For You discounted
What’s The Current Job Market For Window Repairs Leeds Professionals?
window Repairs leeds
10 Facts About CSGO Weapon Case That Can Instantly Put You In Good Mood
esports 2013 winter case [linkagogo.trade]
Watch Out: How Counter-Strike Offensive Global Is Taking Over And
How To Respond Revolver Case
I don’t know if it’s just me or if everybody else experiencing issues with your blog.
It seems like some of the text in your content are running off the screen.
Can somebody else please provide feedback and let me know if this is happening
to them as well? This may be a problem with my internet browser
because I’ve had this happen before. Kudos
10 Things People Hate About Are CSGO Cases Predetermined Operation hydra Case
With havin so much written content do you ever run into any problems of plagorism or copyright violation?
My blog has a lot of completely unique content I’ve either authored myself or outsourced but it appears a lot of it is popping it up all over
the web without my permission. Do you know any techniques to help prevent content from being ripped off?
I’d certainly appreciate it.
What’s The Current Job Market For Double Glazed Window Repairs Professionals Like?
double glazed window repairs (http://kbphone.co.kr/)
20 Trailblazers Setting The Standard In Double
Glazed Units Manufacturers Near Me installation
10 Facts About Upvc Window Repairs That Will Instantly
Put You In Good Mood Window Repairs Near Me
1. Вибір натяжної стелі: як правильно підібрати?
2. ТОП-5 переваг натяжних стель для вашого інтер’єру
3. Як доглядати за натяжною стелею: корисні поради
4. Натяжні стелі: модний тренд сучасного дизайну
5. Як вибрати кольорову гаму для натяжної стелі?
6. Натяжні стелі від А до Я: основні поняття
7. Комфорт та елегантність: переваги натяжних стель
8. Якість матеріалів для натяжних стель: що обрати?
9. Ефективне освітлення з натяжними стелями: ідеї та поради
10. Натяжні стелі у ванній кімнаті: плюси та мінуси
11. Як відремонтувати натяжну стелю вдома: поетапна інструкція
12. Візуальні ефекти з допомогою натяжних стель: ідеї дизайну
13. Натяжні стелі з фотопринтом: оригінальний дизайн для вашого інтер’єру
14. Готові або індивідуальні: які натяжні стелі обрати?
15. Натяжні стелі у спальні: як створити атмосферу затишку
16. Вигода та функціональність: чому варто встановити натяжну стелю?
17. Натяжні стелі у кухні: практичність та естетика поєднуються
18. Різновиди кріплень для натяжних стель: який обрати?
19. Комплектація натяжних стель: що потрібно знати при виборі
20. Натяжні стелі зі звукоізоляцією: комфорт та тиша у вашому будинку!
тканинна натяжна стеля ціна https://natyazhnistelidfvf.kiev.ua/ .
What’s The Good And Bad About Mazda Keys Replacement
Mazda Key Fob
A Complete Guide To Unlock My Car Service keys locked in Car how to unlock