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
Why Adding Vehicle Diagnostics To Your Life’s Activities Will
Make All The Different Diagnostics Mobile
Responsible For A Semi Truck Lawsuits Budget? 10 Incredible Ways To Spend Your Money Semi truck accident Lawsuit
Guide To Dangerous Drugs Lawyers: The Intermediate Guide The Steps To Dangerous
Drugs Lawyers Dangerous drugs Lawyer
Ten Apps To Help Manage Your Injury Attorney injury Lawyers
11 “Faux Pas” You’re Actually Able To Do With Your Slot Competitions Best Payouts Slots
buy cytotec over the counter: buy cytotec over the counter – Cytotec 200mcg price
https://ciprofloxacin.tech/# cipro ciprofloxacin
The Main Problem With Mesothelioma Claim And How You Can Solve It mesothelioma Litigation
lisinopril oral: lisinopril price in india – lisinopril tablets for sale
http://ciprofloxacin.tech/# buy ciprofloxacin over the counter
You’ll Never Guess This Mesothelioma Legal Question’s
Benefits mesothelioma legal question (0522565551.Ussoft.kr)
cytotec abortion pill: cytotec abortion pill – buy misoprostol over the counter
Thanks in favor of sharing such a good thought, paragraph is fastidious, thats why i have read it completely
Here is my web page :: Sex-Podcast
I have to thank you for the efforts you’ve put in penning this site.
I’m hoping to see the same high-grade blog posts by you later on as well.
In fact, your creative writing abilities has motivated me to get my very own site now 😉
My web page … gold-filled necklaces
http://lisinopril.network/# lisinopril 10 mg price
10 Top Mobile Apps For Double Glazing Door
Repairs door repair
lisinopril 30: https://lisinoprilpharm.com/%5Dlisinopril – buy lisinopril 20 mg online
What Is It That Makes Car Diagnostic Near Me So Popular?
vauxhall diagnostics near me
cytotec abortion pill: Misoprostol 200 mg buy online – Abortion pills online
get generic propecia without a prescription buy propecia tablets buy propecia price
10 Things That Your Family Taught You About Examples Of Online Shopping examples of online shopping
how to get nolvadex: nolvadex online – tamoxifen moa
Your method of telling all in this post is truly fastidious, all
be capable of without difficulty know it, Thanks a lot https://www.transportrbeaudet.ca/fr/drain-francais
See What Audi Replacement Key Service Near Me Tricks
The Celebs Are Using Near Me
BBFly Joyn video Downloader ist mit allen Versionen von Windows
OS kompatibel und verlangsamt die Leistung Ihres Computers nicht.
вулкан платинум онлайн
This Week’s Most Popular Stories About Treadmill Foldable Treadmill Foldable treadmills Foldable
get generic propecia prices: get generic propecia without rx – cost generic propecia pill
I think the admin of this web site is actually working hard for his
web page, for the reason that here every data is quality based information.
tamoxifen generic tamoxifen benefits tamoxifen rash