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
https://casinositeleri.win/# casino siteleri win
deneme bonusu veren yeni siteler
Быстрое обучение и получение диплома магистра – возможно ли это?
https://ultrabet-tr.online/# ultrabet
deneme bonusu veren siteler
ultrabet ultrabet ultrabet tr online
Диплом вуза купить официально с упрощенным обучением в Москве
Hello There. I found your blog using msn. This is an extremely well written article.
I’ll make sure to bookmark it and come back to
read more of your useful information. Thanks for the post.
I’ll definitely comeback.
10 Facts About 3 Wheeled Pushchair That Will Instantly Put You In A Good Mood 3 wheeled Strollers
deneme bonusu veren yeni siteler http://ultrabet-tr.online/# ultrabet bonus
https://matadorbet.bid/# matadorbet bid
deneme bonusu veren siteler yeni
Medicament information leaflet. Generic Name.
how to buy cheap cephalexin
Actual information about medicines. Get information here.
ラブドール えろYou might think you’re likely to eat more when you’re really hungry or if you’re digging in to one of your favorite dishes.But that’s not always the case.
deneme bonusu veren siteler mycbet.com deneme bonusu veren siteler betturkey betturkey.com deneme bonusu veren siteler
slot oyunlar? puf noktalar?: slot oyunlar? – slot tr online
Why You Should Focus On Making Improvements Bentley Valet Key New bentley Key
Как получить диплом техникума с упрощенным обучением в Москве официально
filcherp.tforums.org/viewtopic.php?f=2&t=31222
WOW just what I was searching for. Came here by searching for glucotrust benefits
Great article, exactly what I wanted to find.
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
rakisochi.ru/users/20
Godfather789ยเว็บตรงของคนไทย เจ้าพ่อมาเฟียเว็บใหญ่ไม่มีโกง
What’s The Job Market For Automatic Folding Travel Mobility Scooter Professionals Like?
automatic Folding Travel mobility scooter
ultrabet tr online: ultrabet giris – ultrabet yeni giris 1125
can i purchase generic prandin without prescription
I like what you guys are usually up too. This type of clever
work and reporting! Keep up the fantastic works
guys I’ve included you guys to my blogroll.
deneme bonusu veren yeni siteler: deneme bonusu veren siteler – deneme bonusu veren siteler denemebonusu2026.com
Drugs information for patients. What side effects can this medication cause?
cephalexinpve
Actual about medicine. Get information now.
The 12 Most Unpleasant Types Of Bean To Cup Machine Accounts You
Follow On Twitter Which Bean To Cup
What a data of un-ambiguity and preserveness of valuable know-how about unpredicted feelings.
3 Wheel Aluminum Rollator 10 Things I’d Love To Have Known Earlier durable rollator – https://yogaasanas.science/wiki/This_Is_The_Advanced_Guide_To_Rollator_Walker_Type_3,
Узнайте стоимость диплома высшего и среднего образования и процесс получения
files.4adm.ru/viewtopic.php?f=2&t=803
Тут можно преобрести купить шкаф для ружья сейфы для оружия москва