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
20 Myths About Popular Slots: Dispelled fun slots
Medicament information sheet. Generic Name.
can you buy generic zofran no prescription
All what you want to know about drugs. Get now.
Hi, i think that i noticed you visited my site thus i came to ?return the want?.I’m attempting to to find things to enhance my site!I suppose its ok to make use of a few of your ideas!!
9 . What Your Parents Taught You About Best
Online Clothing Sites Uk Best Online clothing Sites uk (sitesco.ru)
Drugs information sheet. Short-Term Effects.
can i purchase generic rizatriptan without rx
Some what you want to know about drug. Get now.
20 Trailblazers Leading The Way In Slot Wins slots with bonuses [Blythe]
9 Things Your Parents Taught You About Mesothelioma Lawsuits mesothelioma lawsuit, Juliane,
Responsible For A Upvc Window Repair Near Me Budget?
12 Top Ways To Spend Your Money upvc window repairs near me (Quentin)
bookmarked!!, I really like your web site!
Have a look at my blog – growth matrix
Are You In Search Of Inspiration? Try Looking Up Low Limit Slots Best payouts slots
My brother recommended I would possibly like this blog.
He was entirely right. This post actually made my day.
You can not imagine simply how much time I had spent for this info!
Thank you!
My web page – hookups near me
10 Best Mobile Apps For Slot Bonuses bonus slots
The 10 Scariest Things About Truck Accident Law Firm Truck accident Law firm
Drugs prescribing information. Long-Term Effects.
nolvadex rx
Everything information about meds. Get now.
How To Make A Successful Upvc External Doors Instructions For Homeschoolers From Home upvc
doors price – Christina,
can you get doxycycline without insurance
Your means of describing all in this article is
actually pleasant, all be able to simply know it, Thanks a
lot.
Drug prescribing information. Brand names.
can i order aldactone without insurance
Actual information about meds. Get information now.
You’ll Never Guess This Winning Slots’s Benefits Winning slots
get cheap imdur prices
It is important that attorneys know how to identify Asbestos attorney products in every case.
This can be accomplished by talking to colleagues, collecting
documents, or analyzing samples taken from home or workplaces.
Speak “Yes” To These 5 Slot Mechanics Tips slot Machine games
order betnovate without insurance
can i get generic doxycycline without rx
How To Beat Your Boss On Double Glazing Supplies Near
Me double glazed glass units, m.thehaedam.com,
A fascinating discussion is worth comment. There’s no doubt that that you should write more about this topic,
it may not be a taboo subject but usually people don’t discuss
such issues. To the next! Kind regards!!
I am regular visitor, how are you everybody? This paragraph posted at this site is in fact good.
Definitely consider that that you stated. Your favourite justification seemed to be
on the net the easiest thing to have in mind of. I say to
you, I certainly get annoyed while other folks consider worries that they just don’t realize
about. You managed to hit the nail upon the highest
and also defined out the entire thing without having
side-effects , other people can take a signal. Will probably be back to get more.
Thank you
What’s The Job Market For Which Is The Best Online Supermarket Professionals?
Which is the best Online supermarket (textpert.hu)