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
Hi, I check your blogs on a regular basis. Your writing style is
awesome, keep doing what you’re doing!
Great tips! For more, visit رنگ ضد حریق
en cok kazand?ran slot oyunlar?: en cok kazand?ran slot oyunlar? – slot oyunlar?
Casino Siteleri: Casino Siteleri – deneme bonusu veren casino siteleri
Nice blog here! Also your site rather a lot up fast!
What web host are you the usage of? Can I am getting your affiliate link on your host?
I desire my website loaded up as quickly as yours lol
화명동에서 저희만큼 돈 많이 버는 곳 없어요~ 화명동 노래방알바에서 돈 좀 벌어 보실래요?
https://casinositeleri25.com/# casino bahis siteleri
Know more about ED at mesalamine-induced acute intolerance syndrome at competitive prices when you purchase from known online
slot siteleri: slot siteleri – en kazancl? slot oyunlar?
Moving is tough Commercial Moves
I truly love your website.. Great colors & theme.
Did you build this amazing site yourself? Please reply back as I’m trying to create my
own site and want to find out where you got this from or what the
theme is named. Thank you!
I don’t even know the way I finished up here, however
I believed this put up was once good. I do not know who you are however definitely you are going to a
famous blogger in case you are not already. Cheers!
slot siteleri slot siteleri slot oyunlar?
This was highly educational. For more, visit roof repair Mechanicsville
WOW just what I was looking for. Came here by searching for LK21
HD
Right now it looks like Expression Engine is the preferred blogging platform available right now.
(from what I’ve read) Is that what you are using on your blog?
I am actually happy to read this website posts which carries plenty of
useful information, thanks for providing these statistics.
Party Scene 부달사이트
http://slotsiteleri25.com/# slot casino siteleri
sweet bonanza oyna sweet bonanza sweet bonanza oyna
Pretty! This has been an incredibly wonderful article.
Thank you for providing this info.
Oh my goodness! Amazing article dude! Thank you so much, However
I am encountering troubles with your RSS. I don’t understand why I cannot subscribe to it.
Is there anyone else having identical RSS problems? Anybody who
knows the solution will you kindly respond? Thanks!!
Visit my website; best decaf tea
This is a topic which is near to my heart… Cheers!
Exactly where are your contact details though?
I get pleasure from, result in I found exactly what I used
to be taking a look for. You’ve ended my 4 day long hunt!
God Bless you man. Have a great day. Bye
Hi to every single one, it’s really a fastidious for me to go
to see this website, it consists of helpful Information.
Hello to all, how is everything, I think every one is getting more from this web site, and your views are nice designed for
new users.
Its like you read my mind! You seem to know a lot approximately this, such as you wrote the book in it or something.
I think that you simply could do with a few p.c. to drive the message house a bit, however other than that, that is wonderful blog.
An excellent read. I’ll certainly be back.
en cok kazand?ran slot oyunlar?: slot siteleri – en cok kazand?ran slot oyunlar?
Займы в Казахстане онлайн — быстрое оформление, минимальные требования, выгодные условия для получения нужной суммы микрокредиты онлайн
https://digi116sa.z31.web.core.windows.net/research/digi116sa-(363).html
Floral prints and delicate colors play well with decor that is sure to embrace the blooms of the spring and summer time months.