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
What’s Holding Back The Buy Our C Driving License Gothenburg
Industry? Köpa C Körkort (Ada)
denemebonusuverensiteler25: deneme bonusu veren siteler yeni – yeni deneme bonusu veren siteler
canl? casino siteleri canl? casino siteleri guvenilir casino siteleri
поставить капельницу от запоя на дому цена поставить капельницу от запоя на дому цена .
вывод из запоя ростовская область вывод из запоя ростовская область .
제의2 수도 부산!! 왜 제 2의 수도인지 저희 부산호빠가 제대로 보여드립니다.
セックス ロボットrewriting history,and the list goes on.
I like the helpful info you provide on your articles. I will bookmark your weblog and take a look at once more here frequently.
I am moderately sure I will learn a lot of new stuff right here!
Best of luck for the following!
“The staff extremely took their time to help me find the suitable fit at flooring in winnipeg
вывод из запоя цены ростов на дону вывод из запоя цены ростов на дону .
denemebonusuverensiteler25 deneme bonusu veren siteler deneme bonusu veren siteler yeni
вывести из запоя анонимно вывести из запоя анонимно .
I do consider all the concepts you’ve introduced to your post.
They are very convincing and will definitely work.
Nonetheless, the posts are very brief for starters.
May you please prolong them a little from subsequent time?
Thanks for the post.
My blog … floor protection
Amazing! Its genuinely awesome paragraph, I have got much clear idea concerning from this piece of writing.
Nice post. I was checking constantly this blog and I am impressed!
Very useful info particularly the last part 🙂 I care for such info much.
I was seeking this particular info for a long time. Thank you and good luck.
Howdy, I do think your website could be having internet browser compatibility problems.
Whenever I take a look at your site in Safari, it looks fine however when opening
in Internet Explorer, it’s got some overlapping issues.
I merely wanted to give you a quick heads up! Other than that, excellent site!
My website: chapel of the flowers
вывод из запоя недорого ростов вывод из запоя недорого ростов .
I value these insights into dealing with possible pipes crises proactively– especially stressing partnership between property owners 24/7 emergency plumber in Phoenix
en kazancl? slot oyunlar?: slot siteleri – slot siteleri
Routine monitoring and follow-ups are likewise important for safety and efficiency.
I took my cell into ### anykeyword ### the day gone by after dropping it Phone repair Boondall
Dermal fillers are another option for wrinkle decrease, claimed Nikhil Dhingra, MD, of
Spring Street Dermatology.
The before-and-after photos of your deck project were stunning! It’s inspiring to see such results. Find more transformations at sunroom installation near me
Private Club 강남오피 (https://www.valeurs-francaises.Fr/web/Valeurs-de-la-Chanson-francaise.html)
Semaglutide and tirzepatide are type 2 diabetic issues drugs
used off-label for weight management.
Activating reduced power setting lessens power usage by limiting some phone functions.
Spot on with this write-up, I honestly believe this website needs much more attention. I’ll probably be
returning to see more, thanks for the advice!
http://denemebonusuverensiteler25.com/# deneme bonusu veren siteler
https://casinositeleri25.com/# Casino Siteleri
http://sweetbonanza25.com/# sweet bonanza oyna