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
Your way of describing the whole thing in this post is genuinely good, all be capable of
easily understand it, Thanks a lot https://www.cabanoneco.ca/
pharmacie en ligne france pas cher: Cialis sans ordonnance pas cher – pharmacie en ligne france livraison internationale
Gas-Dank Delivery is dedicated to providing marijuana to meet any budget and preference https://dojos.ca/ct.ashx?t=https://www.devs.ng/
http://pharmaciepascher.pro/# pharmacie en ligne france livraison internationale
Профессиональный сервисный центр по ремонту плоттеров в Москве.
Мы предлагаем: ремонт плоттеров с гарантией
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Viagra prix pharmacie paris: Viagra Pfizer sans ordonnance – Prix du Viagra 100mg en France
Viagra sans ordonnance pharmacie France: Acheter du Viagra sans ordonnance – Viagra sans ordonnance 24h suisse
Wow! This blog looks exactly like my old one!
It’s on a totally different topic but it has pretty much the
same page layout and design. Wonderful choice of colors!
pharmacie en ligne: Acheter Cialis – pharmacie en ligne
Your means of describing all in this article is really pleasant, all can easily know it,
Thanks a lot https://abrasifsea.com/roues-et-disques-abrasifs/
This text is invaluable. Where can I find out more?
вход вавада
The 10 Scariest Things About Link Togel Link togel
pharmacie en ligne: cialis generique – pharmacie en ligne pas cher
http://vgrsansordonnance.com/# Viagra sans ordonnance livraison 24h
Thank you for sharing your info. I really appreciate your efforts and I am waiting for your further write ups thanks once again.
my website … foundation repair Dallas-Fort Worth
http://vgrsansordonnance.com/# Viagra femme ou trouver
Introduction: https://greenglucose.net/ Glucose has been garnering a lot of attention in the health and wellness community for its reported benefits in managing blood sugar levels.
What’s up it’s me, I am also visiting this web site on a regular basis, this web page is genuinely pleasant and the
people are actually sharing nice thoughts.
Hey There. I discovered your weblog the use of msn. That is an extremely well written article.
I’ll make sure to bookmark it and return to learn extra of your helpful info.
Thank you for the post. I will certainly comeback.
In the increasingly crowded marketplace of health supplements, one product that has garnered significant attention is http://whoops.tium.co.kr/yc/bbs/board.php?bo_table=notice&wr_id=353080.
Продажа Смартфонов https://elektronik-art.ru Apple iPhone (Эпл Айфон) цены ниже конкурентов.
Pharmacie sans ordonnance: cialis sans ordonnance – pharmacie en ligne pas cher
Профессиональный сервисный центр по ремонту плоттеров в Москве.
Мы предлагаем: цены на ремонт плоттеров
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Excellent blog! Do you have any recommendations for
aspiring writers? I’m planning to start my own blog soon but I’m a little lost on everything.
Would you propose starting with a free platform like WordPress or go
for a paid option? There are so many options out
there that I’m totally confused .. Any tips? Bless you!
Ιt’ѕ remarkable tօ visit tһіs web page and reading tһe views of ɑll
friends concerning tһis post, wһile I am alsօ zeaous of getting knoԝ-how.
My homеⲣage; beli viewers youtube murah
This Is The History Of Psychiatrist Assessment Uk In 10 Milestones psychiatry uk adhd self Assessment
It is in reality a nice and helpful piece of info. I’m glad that you shared this useful
info with us. Please keep us informed like this.
Thanks for sharing.
Generally I do not read article on blogs, however
I would like to say that this write-up very pressured me to check out and do it!
Your writing taste has been amazed me. Thanks, quite great article.
Also visit my blog … foundation repair DFW
trouver un mГ©dicament en pharmacie cialis generique vente de mГ©dicament en ligne