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
The intelligence is the most effective dry
herb Vegetable Based Vape Juice I have actually tried.
In this instance, the life insurance policy company’s money.
Also visit my website … ai picture generator reddit free
It’s not my first time to go to see this website, i am visiting this site dailly and get good information from here every day.
Allow me show you what causes decay in food my review of NewULife.
Pero después de la Primera Guerra Mundial la construcción quedó paralizada y solo se habilitó una parte del conjunto proyectado.
Su chasis es un monocasco de fibra de carbono y en su construcción también se utiliza numerosos
componentes, como: titanio, magnesio y kevlar, para ofrecer una carrocería ligera y con gran resistencia a las
fuerzas de gravedad que se generaban en su andar y, por si no fuera poco, parte del motor y
cofre se cubrían con un baño de oro de 24 kilates y no solamente por
capricho, ya que se trata de un material que disipa mejor el calor generado.
El representante permanente palestino ante la ONU, Riad Mansur, indicó
que catorce de los quince miembros del Consejo de Seguridad habían respaldado el comunicado, con la única excepción de Estados Unidos, que utilizó
su derecho de veto. Finalmente el 28 de marzo, cuatro días después, se firmó el decreto
N.º 4870, estableciendo la creación del Partido de Coronel de Marina Leonardo Rosales, para regocijo de los miembros de la Comisión Pro Autonomía, presentes en dicho
acto, y toda la población. También en 1945, al crearse el Partido de Coronel Rosales, la nueva municipalidad se
vio obligada a incorporar un gran número de agentes para
cumplir con sus tareas propias.
How To Save Money On Double Glazed Window Repairs Near Me double glassing
– http://jbnucri.com/bbs/board.php?bo_table=companylist&wr_id=13339 –
XYGENYX, a licensing company for FDA-registered products.
Visit my web page … Bookmarks
USP human development hormonal agent (somatropin).
Review my homepage :: ai portrait generator reddit
Joy Organics is considered a premium where to buy broad spectrum cbd gummies company.
NewULife Ranks. Invite to my NewUlife Evaluation.
my blog: Bookmarks; Mackenzie,
Good info. Lucky me I discovered your website by accident (stumbleupon).
I have book-marked it for later!
Drugs information for patients. What side effects can this medication cause?
cost of colchicine
Everything what you want to know about pills. Get now.
I don’t even know the way I stopped up here, however I believed this put up
used to be good. I don’t recognise who you’re however definitely you’re going to a famous blogger in case you are not
already 😉 Cheers!
Also visit my web blog best SEO services
The IQ is the very best dry herb vape I have attempted.
my web blog; plant based alternative to vaping
Allow me show you in my review of NewULife.
Review my website … Bookmarks
Portable vaporizers use batteries as a source of
power.
Also visit my web site … plant based vape canada
where to get cheap inderal no prescription
Watch Out: What Window Repair Is Taking Over And What You Can Do About It https://www.257634.xyz
Meds prescribing information. Long-Term Effects.
can i buy nolvadex no prescription
All news about drug. Read now.
5 Killer Quora Answers To Window Replacement Near Me window Replacement Near Me
15 Shocking Facts About Replace Bmw Key You’ve Never Heard Of bmw 1 series Replacement Key
The intelligence is the very best dry natural herb vape
I have tried.
my website – Bookmarks
Check Out: How Mesothelioma Asbestos Claims Is Taking Over And What Can We Do
About It Cassy Lawn
What is New U Life SOMADERM Transdermal Gel.
Here is my blog … Best Motion Graphics Courses Online
Drugs information sheet. Generic Name.
cost minocycline without dr prescription
Actual information about pills. Read here.
7 Simple Tips To Totally Rocking Your Online Shopping Sites London best luxury online shopping sites uk
Drugs information. Short-Term Effects.
where to get generic lansoprazole without prescription
Actual trends of medicine. Read here.
NewULife Ranks. Welcome to my NewUlife Review.
Feel free to surf to my web blog Bookmarks
Hurrah, that’s what I was exploring for, what a stuff!
present here at this web site, thanks admin of this web page.
It’s really a great and helpful piece of info. I’m glad that you shared this helpful information with us.
Please stay us up to date like this. Thanks for sharing.