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
вывод из запоя дешево ростов-на-дону вывод из запоя дешево ростов-на-дону .
Medical spas have become my go-to for self-care days; they combine relaxation with results! Lotus Health and Aesthetics
I know this if off topic but I’m looking into starting my
own blog and was wondering what all is required to get setup?
I’m assuming having a blog like yours would cost a pretty penny?
I’m not very internet savvy so I’m not 100% certain. Any suggestions or advice would be greatly appreciated.
Thanks
Hey I know this is off topic but I was wondering if you knew of any
widgets I could add to my blog that automatically tweet
my newest twitter updates. I’ve been looking
for a plug-in like this for quite some time and was hoping maybe you
would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.
I can not believe just how simple it was to lease a bounce home online! The procedure was smooth and simple with bounce house rental
While this was informative, I believe certain aspects were overlooked.
Just booked my first consultation at a local medical spa custom facials
casino bahis siteleri: Casino Siteleri – Canl? Casino Siteleri
en guvenilir casino siteleri: Casino Siteleri – casino bahis siteleri
9 Signs That You’re The Buy Driver’s License B96 Without A
Degree Online Expert KöPa Ett KöRkort
I appreciate, lead to I discovered exactly what I was
having a look for. You have ended my four day long hunt!
God Bless you man. Have a nice day. Bye
10 Great Ideas For Party And Wedding Entertainment 부달 (marvelvsdc.Faith)
Hello there! This is my first comment here so I just wanted to give
a quick shout out and tell you I genuinely enjoy reading your blog posts.
Can you recommend any other blogs/websites/forums that deal with the same topics?
Appreciate it!
Great information. Lucky me I found your blog by
accident (stumbleupon). I have saved as a favorite for later!
slot oyunlar?: slot casino siteleri – en kazancl? slot oyunlar?
en guvenilir casino siteleri: en guvenilir casino siteleri – casino bahis siteleri
Hi there just wanted to give you a quick heads
up. The text in your post seem to be running off the screen in Opera.
I’m not sure if this is a format issue or something to do with internet browser compatibility but I thought I’d post to let you know.
The design and style look great though! Hope you get the problem solved soon. Many thanks
Hey! Quick question that’s totally off topic. Do you
know how to make your site mobile friendly?
My web site looks weird when browsing from my apple iphone.
I’m trying to find a template or plugin that might be able to correct this problem.
If you have any recommendations, please share. Thank
you!
Thanks for breaking down the cost factors associated with different types of roofs; budgeting is so important—check out additional resources at roofing company
кухни от производителя — покупайте мебель напрямую от проверенного производителя.
Thanks for the clear advice. More at dump trailers for sale near me
This was a wonderful guide. Check out رنگ ضد حریق for more
14 Creative Ways To Spend On Leftover Chest Freezer Near Me Budget
Electriq chest Freezer
Canl? Casino Siteleri: canl? casino siteleri – en guvenilir casino siteleri
great submit, very informative. I’m wondering why the other specialists of this sector don’t understand this.
You should proceed your writing. I’m confident,
you’ve a huge readers’ base already!
you could look here https://granbyma.net/2024/06/13/keberuntungan-besar-di-mio88-slot-menangkan-jackpot-impian-anda/
gГјvenilir kumar siteleri: canl? casino siteleri – Casino Siteleri
вывод из запоя цена на дому вывод из запоя цена на дому .
Casino Siteleri Casino Siteleri Deneme Bonusu Veren Siteler
вывести из запоя вывести из запоя .