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
11 “Faux Pas” You’re Actually Able To Make With Your Wall Mount Fireplaces https://www.lynnbolvin.top/0siaqo-ci09yis-k943-b7l-mfnp0t-1848
Meds information leaflet. What side effects?
order keflex online
Some what you want to know about medicines. Get now.
You’ll Never Be Able To Figure Out This Veterans Disability Settlement’s Benefits Veterans Disability
Hi! canadian online pharmacy no prescription very good web site.
10 Websites To Help You Be A Pro In Malpractice Attorneys Malpractice law firms
Guide To Double Glazed Window Near Me: The Intermediate Guide Towards Double
Glazed Window Near Me Double Glazed Window Near Me
Medicines information for patients. What side effects can this medication cause?
where to get generic cytotec without rx
Actual information about medicines. Get information here.
12 Companies Are Leading The Way In Upvc Windows And
Doors repair Upvc window
Добрый день!
Приобрести документ о получении высшего образования вы имеете возможность у нас в Москве. Мы предлагаем документы об окончании любых университетов РФ. Вы получите необходимый диплом по любым специальностям, включая документы старого образца. Даем 100% гарантию, что в случае проверки документов работодателями, никаких подозрений не появится.
Наши специалисты предлагают максимально быстро приобрести диплом, который выполняется на оригинальном бланке и заверен печатями, штампами, подписями официальных лиц. Диплом пройдет лубую проверку, даже с применением профессиональных приборов. Достигайте свои цели быстро и просто с нашими дипломами.
Где заказать диплом специалиста?
https://teplo-hata.com.ua/users/uqonuxuk
Успехов в учебе!
The Three Greatest Moments In Personal Injury
Litigation History personal Injury lawyer
The Top 5 Reasons People Win Within The Slot Demo Princess 1000 Industry demo slot pragmatic play princess (https://dejesus-hewitt-3.blogbright.net)
Glory Casino
Best Search Engines Optimization Tips – 5 Simple Seo
Wordpress Techniques 워드프레스 seo
hey there and thank you for your info – I’ve definitely picked up something new from
right here. I did however expertise several technical issues using this web site, as I experienced to reload the web site many times previous to I could get it to load correctly.
I had been wondering if your web host is OK? Not that I am complaining,
but sluggish loading instances times will very frequently affect your placement
in google and could damage your quality score if ads and marketing with Adwords.
Well I’m adding this RSS to my email and could look out for a
lot more of your respective exciting content. Ensure that
you update this again very soon.
Ten Kids Bunk Beds That Really Help You Live Better Kids bunk Beds
The Reasons You’re Not Successing At Assessment Of Adult Adhd adhd diagnostic assessment For Adults
https://justpaste.me/22Fv
SEO Expert San Diego
Good way of telling, and fastidious piece of writing to obtain data regarding my presentation subject,
which i am going to convey in academy.
10 Boat Accident Lawyer Tricks All Experts Recommend boat Accident Lawyers
The Most Effective Advice You’ll Ever Get About Upvc Door And Windows Window handle Upvc
20 Fun Facts About Double Glazing Repair jerealas
A Glimpse Inside The Secrets Of Sweet Bonanza Xmas Slot Demo Sweet bonanza free play demo
A Provocative Rant About Akun Demo Slot Game demo Slot
10 Tips For Upvc Repairs Near Me That Are Unexpected window repair near me (http://Ns1.javset.net/)
Medicine information for patients. Generic Name.
buy generic oxcarbazepine price
Actual trends of medicament. Get information now.
You’ll Never Guess This Veterans Disability Settlement’s Tricks Veterans Disability
Hi there, I discovered your website via Google at the same time as searching for a related subject, your web site got here up, it looks great. I’ve bookmarked it in my google bookmarks.
Appreciate the recommendation. Will try it out.
Outstanding post however I was wanting to know if you could write a litte more on this topic?
I’d be very grateful if you could elaborate a little bit further.
Many thanks!
My blog post; http://superpremium2.premium4best.eu/
You’ll Never Guess This Window Repair Near Me’s Benefits Window repair near me
10 Best Mobile Apps For Veterans Disability Law
Veterans Disability lawyers