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
deneme bonusu veren casino siteleri deneme bonusu veren casino siteleri guvenilir casino siteleri
Nine Things That Your Parent Teach You About Accident Attorneys In My
Area accident attorneys (Josefa)
en cok kazand?ran slot oyunlar?: slot oyunlar? – slot siteleri
slot oyunlar? slot oyunlar? puf noktalar? slot casino siteleri
Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.
First off I would like to say terrific blog! I had a quick question in which I’d
like to ask if you don’t mind. I was curious to know
how you center yourself and clear your head before writing.
I’ve had trouble clearing my mind in getting my ideas out there.
I do take pleasure in writing however it just seems like the first 10 to
15 minutes tend to be wasted simply just trying to
figure out how to begin. Any suggestions or tips? Appreciate it!
Terima kasih atas informasinya! Kebetulan saya menemukan pembahasan serupa di KADEKTOTO SLOT GACOR.
I’m gone to tell my little brother, that he should also pay
a quick visit this web site on regular basis to obtain updated from most up-to-date reports. https://Kv-Work.com/bbs/board.php?bo_table=free&wr_id=1747206
Your mode of explaining everything in this post is genuinely pleasant, all can simply be aware of it, Thanks a lot.
Terima kasih atas informasinya! Kebetulan saya menemukan pembahasan serupa di KADEKTOTO SLOT GACOR.
It’s hard finding trustworthy contractors; happy that I’ve found Home remodeling cypress tx
бонусы
Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.
slot oyunlar? puf noktalar?: slot casino siteleri – en kazancl? slot oyunlar?
darkmarkets how to get on dark web dark market onion
Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.
Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.
VIP Lounge 하이오피
ArtiVery interesting article! I also just read the related article on KADEKTOTO SLOT GACOR which discusses similar topics.
提供WPS下载,适配所有主流平台,包括Windows、Mac、Android和iOS。WPS Office提供全功能的文字处理、表格编辑、PPT制作和PDF管理工具,兼容Microsoft Office文件格式。免费下载WPS,享受高效便捷的办公体验,无论是个人用户还是团队协作,都能轻松应对。https://www.wps2.com/ rijpcuvgxm
slotozal casino
Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.
These people were previously scheduled for tummy tuck prior to undergoing HIFU therapy.
Thanks for the clear breakdown. Find more at adi yoga yoga instructor mumbai
Medicament information sheet. Short-Term Effects.
lisinopril hctz a 28
Best trends of drugs. Read here.
Pembahasan yang bagus! Untuk informasi tambahan, Anda bisa mengunjungi KADEKTOTO SLOT GACOR.
sweet bonanza: sweet bonanza – sweet bonanza
Great insights into color choices for asphalt paving projects! Color can really change the aesthetic appeal of an area! Explore more at asphalt company jacksonville
Terima kasih atas informasinya! Kebetulan saya menemukan pembahasan serupa di KADEKTOTO SLOT GACOR.
Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.