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
prednisone 2.5 mg cost: order Prednisone – where to buy prednisone uk
prednisone 12 mg: ray pharm – generic over the counter prednisone
payday loan
buy amoxicillin without prescription: Amoxicillin Com Pharm – amoxicillin 500mg capsule
Официальная покупка диплома вуза с упрощенной программой обучения
tor markets 2024 best darknet markets https://darknetmarketstore.com/ – dark website
where can i get cheap clomid without dr prescription: clomid purchase online rex pharm – where can i get generic clomid without insurance
prednisone 5 50mg tablet price: prednisone – order prednisone online canada
prednisone 50 mg tablet canada: prednisone ray pharm – prednisone 30 mg daily
Magnificent items from you, man. I’ve bear in mind your stuff
previous to and you are simply too fantastic. I actually like what you’ve acquired here, really like what you are stating and the best way
wherein you say it. You’re making it enjoyable and you continue to take
care of to stay it sensible. I cant wait to read much more from you.
That is really a tremendous website.
I know this web site provides quality depending articles and other material, is there any other web page which provides
these data in quality?
Hi there, yes this article is actually fastidious and I have learned lot of things from it concerning blogging.
thanks.
Also visit my webpage … Daycare Near Me – Find The Best Daycares Near You
Hi, of course this piece of writing is really good and I have learned lot of things from it
on the topic of blogging. thanks.
buy priligy: max pharm – buy priligy
Как избежать рисков при покупке диплома колледжа или ПТУ в России
Very rapidly this website will be famous among all blogging and site-building users,
due to it’s fastidious posts
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
Medication prescribing information. What side effects?
optimal dosage of cymbalta understanding the benefits of 40 mg once daily
Some about medicament. Get information now.
Полезные советы по безопасной покупке диплома о высшем образовании
You can definitely see your expertise in the article you write.
The arena hopes for even more passionate writers such as you who aren’t afraid to say how they believe.
All the time follow your heart.
compare prednisone prices: raypharm – prednisone 10mg tabs
max pharm: priligy maxpharm – dapoxetine online
tsmk-altai.ru/ – Закажите кухню, которая станет настоящей гордостью вашего дома.
Thank you for another informative web site.
The place else may just I get that type of information written in such an ideal way?
I have a undertaking that I’m simply now running on,
and I have been at the glance out for such info.
where to buy generic clomid: cheap clomid – can i buy cheap clomid without dr prescription
order cheap clomid now: clomid purchase online rex pharm – can you buy generic clomid online
prednisone 20 mg purchase: prednisone – generic prednisone 10mg
Полезная информация как купить диплом о высшем образовании без рисков
non prescription prednisone 20mg: order Prednisone – prednisone pills 10 mg