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
Regards, Ample content!
order prednisone on line prednisone 50 mg price medicine prednisone 5mg
amoxicillin for sale online: cost of amoxicillin prescription – amoxicillin where to get
This was very well put together. Discover more at live casinos providers
deep web links darknet links dark markets 2024
can i buy generic clomid buying cheap clomid without insurance can you buy generic clomid pills
amoxicillin order online no prescription buy amoxicillin cost of amoxicillin 875 mg
Интересные статьи о займах – https://omskpress.ru/news/324878/kak-prosto-vzyat-mikrozajm/
prednisone 10: Predni Best – generic prednisone cost
https://clomidonpharm.com/# where to get generic clomid without dr prescription
I enjoyed this article. Check out 27330 Auto Glass for more
Adult Entertainment 오피커뮤니티
Предлагаем услуги профессиональных инженеров офицальной мастерской.
Еслли вы искали срочный ремонт стиральных машин smeg, можете посмотреть на сайте: ремонт стиральных машин smeg цены
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
выведение из запоя спб выведение из запоя спб .
http://amoxstar.com/# amoxicillin 500 mg capsule
I had the best time hiking in the nearby trails of Santa Rosa CA. Nature is calling! Discover more hikes at professional marketing solutions Santa Rosa
prednisone 2.5 mg cost PredniBest prednisone 40mg
خدمات پسیو شبکه
Hello, I think your site might be having browser compatibility issues.
When I look at your blog site in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up! Other then that, awesome blog!
prednisone without prescription medication: prednisone in india – prednisone 10 mg tablet
Pretty nice post. I just stumbled upon your weblog and wished to
say that I have really enjoyed browsing your
blog posts. In any case I’ll be subscribing to your rss feed and I hope you write again very soon!
amoxicillin 200 mg tablet: AmoxStar – amoxicillin no prescription
can you buy cheap clomid: clomidonpharm – buy generic clomid
выведение из запоя на дому спб цены выведение из запоя на дому спб цены .
where can i buy generic clomid can you buy generic clomid online can i order clomid online
https://clomidonpharm.com/# where to buy cheap clomid without rx
Cheers. Great information.
Hey! Do you use Twitter? I’d like to follow you if that would be okay.
I’m definitely enjoying your blog and look forward to new updates.
Here is my web page – retirement communities
Each state has different laws regarding personal injury claims—make sure your lawyer knows them inside best car accident lawyer near me
I’ll right away take hold of your rss feed as I can’t to find your e-mail subscription hyperlink or e-newsletter service.
Do you have any? Kindly let me realize so that I may subscribe.
Thanks.