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
Canl? Casino Siteleri: guvenilir casino siteleri – Deneme Bonusu Veren Siteler
Appreciate the comprehensive insights. For more, visit dr thakur dental clinic
Thanks for the great information. More at Reliable Kitchen Remodeler Vancouver WA – Cross Home Remodeling
Playing Poker – Find Out Game First 에볼루션 바카라 하는법
Thanks for the helpful advice. Discover more at استخدام نیروی نمایشگاهی
вывод из запоя на дому ростов круглосуточно [url=https://vyvod-iz-zapoya-rostov27.ru/]вывод из запоя на дому ростов круглосуточно[/url] .
Way cool! Some extremely valid points! I appreciate you penning this write-up and also the rest of the site is also really
good.
5 Trucking Lawyer Projects For Every Budget lawyers truck accident near me (lovewiki.faith)
This was a fantastic read. Check out mens salon near me for more
deneme bonusu veren siteler yeni: yeni deneme bonusu veren siteler – yat?r?ms?z deneme bonusu veren siteler
клиника вывод из запоя ростов клиника вывод из запоя ростов .
Great insights! Discover more at رنگ ضد حریق
stamps maker stamps maker .
I appreciated this post. Check out متن روز پدر برای استوری for more
10 Things You Learned In Preschool That’ll Help You Understand
Folding Treadmill With Incline why is incline treadmill good –
http://www.google.co.ls,
http://sweetbonanza25.com/# sweet bonanza guncel
Undeniably believe that which you stated. Your favorite
justification appeared to be on the net the simplest thing to
be aware of. I say to you, I certainly get irked while people consider worries that they plainly do not know about.
You managed to hit the nail upon the top and also defined out the whole thing without having side-effects , people
can take a signal. Will probably be back to get more.
Thanks
Governor Brian Kemp is a staunch opponent of legalized gambling, which is why sports wagering hasn’t taken off in the Peach State.
This was quite enlightening. Check out spa denver for more
Medicines information sheet. Brand names.
can i get neurontin without a prescription
Some what you want to know about medicines. Read information here.
여행 가기 전, 여행 후 저희 김포호빠에서 피로를 한 번 풀어 보시죠
casino bahis siteleri: Casino Siteleri – canl? casino siteleri
Hi to every body, it’s my first visit of this web site; this blog carries amazing and actually
excellent material in favor of readers.
http://www.sklad-ymov.ru — На нашем сайте вы найдете всё необходимое для создания кухни вашей мечты.
I love how distinct your explanation of lock mechanisms is—very wonderful learn! Thanks! locksmith
tor market links dark web sites links dark web sites
Appreciate the great suggestions. For more, visit مقارنة بين الكازينوهات
https://casinositeleri25.com/# guvenilir casino siteleri
https://denemebonusuverensiteler25.com/# deneme bonusu veren siteler
Excellent article! We are linking to this particularly great article on our
site. Keep up the great writing.