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
Полезная информация как купить диплом о высшем образовании без рисков
stamp online maker stamp online maker .
If you prefer a customized adventure in Washington DC, think about making a reservation for an exclusive excursion private limo service
Hey there! Someone in my Myspace group shared this site with us so I came to check it out.
I’m definitely loving the information. I’m
book-marking and will be tweeting this to my followers!
Fantastic blog and fantastic design and style.
9 . What Your Parents Taught You About American Fridge
Freezer Sale american Fridge freezer sale
Adult Entertainment 인천유흥
11 Strategies To Completely Redesign Your Asbestosis Asbestos Mesothelioma Attorney mesothelioma lawsuits (https://lovewiki.faith/wiki/pottshaastrup1490)
I have actually been to numerous Brazilian steakhouses, and every one has its very own unique twist on typical dishes brazilian buffet steakhouse
Полезная информация как купить диплом о высшем образовании без рисков
This was quite helpful. For more, visit cake shop near me
Thanks for the informative post. More at roofing service orlando fl
Massage Therapy: What Just A Few Ingredients To Learn It 유흥 (Salvatore)
нарколог на дом вывод из запоя ростов нарколог на дом вывод из запоя ростов .
Полезные советы по безопасной покупке диплома о высшем образовании
наркология вывод из запоя ростов наркология вывод из запоя ростов .
Used appropriately, a land study will certainly make your development go
as efficiently as possible.
A person essentially assist to make significantly posts I’d state.
That is the first time I frequented your web page and thus far?
I amazed with the analysis you made to create this actual put up extraordinary.
Magnificent activity!
deneme bonusu veren siteler yeni denemebonusuverensiteler25 yeni deneme bonusu veren siteler
en guvenilir casino siteleri: gГјvenilir deneme bonusu veren siteler – deneme bonusu veren casino siteleri
stamps maker [url=https://www.stamp-creator-online0.com]stamps maker[/url] .
An impressive share! I have just forwarded this onto a
friend who has been conducting a little homework
on this. And he actually bought me breakfast simply because I discovered it for him…
lol. So let me reword this…. Thanks for the meal!!
But yeah, thanks for spending some time to discuss this subject here on your internet
site.
If you’re in Houston and need to ship a vehicle Houston vehicle transport
I’m no longer positive where you are getting your info, however good topic.
I needs to spend some time learning much more or figuring out more.
Thank you for excellent info I was in search of this info for my mission.
yeni deneme bonusu veren siteler deneme bonusu veren yeni siteler deneme bonusu veren siteler yeni
вывод из запоя дешево ростов вывод из запоя дешево ростов .
https://denemebonusuverensiteler25.com/# yeni deneme bonusu veren siteler
I enjoyed this article. Check out Roof Repair Mesa for more
слотозал казино
Cyprus is lucky to have such talented contractors like Pool remodelers near me
I am regular reader, how are you everybody? This paragraph posted
at this website is truly fastidious.