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
A Guide To What Is SEO Software From Start To Finish Ranking
The 10 Most Terrifying Things About Upvc Door Repairs Near Me upvc door repairs near me
Also visit my page; https://thedogeverse.io/
The 10 Scariest Things About List Of Online Shopping Sites
Uk list Of online shopping sites Uk (dnpaint.Co.kr)
So , You’ve Purchased Online Shopping Sites Uk …
Now What? Cangshan V2 Series (Jenny)
A Complete Guide To Motor Vehicle Lawyers Dos And Don’ts Motor vehicle Accident lawyers
מהקשר האירוטי. כשאתם יוצאים לדייט עם נערות ליווי ברמת גן, אתם יכולים להיות בטוחים שהפצצות היפהפיות יפהפיות לחלוטין. הרצונות עילית והסביבה הקרובה דירות דיסקרטיות אליהן מגיעים גברים ונשים לבילויים דיסקרטיים תוכלו למצוא בכל רחבי הארץ וגם בנצרת. אלו הן נערות ליווי בטבריה
What’s The Job Market For Double Glazed Repairs Near Me Professionals?
double glazed repairs (Jaunita)
15 Presents For Your Renault Car Key Replacement Near Me Lover In Your Life renault modus key programming
Why You’re Failing At Double Glazing Companies Near Me Double Glazing Windows Replacement
Nine Things That Your Parent Teach You About Window Handles Replacement Window handles replacement (http://verbina-glucharkina.ru/)
Could New Slots Online Be The Key To Achieving 2023? land-Based Slots
Keep on working, great job!
Hi to every body, it’s my first go to see of this website;
this weblog contains amazing and really fine data in support of visitors.
10 Things Everyone Has To Say About Online Casino Slots newest slots (king-bookmark.stream)
The Top Reasons For Truck Accident’s Biggest “Myths” About Truck Accident Could
Actually Be True Truck Accident Lawsuit
I used to be suggested this blog by way of my cousin. I’m not certain whether or not this
publish is written by him as no one else recognise such designated about my trouble.
You are amazing! Thanks!
Incredible quite a lot of superb knowledge!
Medicine information for patients. Cautions.
how to buy generic loperamide without rx
Some information about drug. Get information here.
10 Life Lessons That We Can Learn From What Is The Best Online Shopping In Uk compact ball Mill Grinding Jar
How To Become A Prosperous Does Amazon Ship To Uk When You’re Not Business-Savvy vimeo.Com
20 Insightful Quotes About Bonus Slots new Slots (http://Www.mazafakas.com)
5 Killer Quora Answers To Replacement Double Glazing Units Near Me
double Glazing units Near me
Just desire to say your article is as surprising.
The clearness in your post is just spectacular and i could assume you’re knowledgeable in this subject.
Fine with your permission allow me to grab your feed to stay updated with forthcoming post.
Thank you a million and please carry on the enjoyable work.
See What Rng Slots Tricks The Celebs Are Utilizing
RNG slots
20 Best Tweets Of All Time Concerning Popular Casino Slots online casino slots (https://bookmarks4.Men/)
What’s The Job Market For Starlight Princess Max Win Professionals?
starlight princess [http://www.yayinmall.com]
This Is A Best Slot Payouts Success Story You’ll Never Be Able
To best online slots
15 Reasons To Not Be Ignoring Window Repairs Near Me
Upvc window repairs near me
10 Tips To Build Your Demo Slot Sugar Rush Empire sugar rush Pragmatic demo