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
купить диплом колледжа
https://fastpillseasy.com/# where to get ed pills
загранпаспорт срочно москва [url=https://www.astana.forum24.ru/?1-0-0-00000564-000-0-0-1735286925]загранпаспорт срочно москва[/url] .
вызов нарколога на дом частная скорая помощь вызов нарколога на дом частная скорая помощь .
카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트
Thanks for the great explanation. More info at services offered by Sculpted MD
Culture Shock And International Business – Tips For Women hiop
The tips shared here are invaluable! For those needing repair services after water damage, try contacting water damage restoration
10 In Order To Look When Ever Choosing A Hen Party Venue 광주유흥 (Buster)
I think finding the perfect salon is essential for any individual that loves their hair! For pointers and recommendations, I very recommend going to Brazilian Blowout SF Keratin Treatment
https://fastpillseasy.com/# how to get ed pills
наркологическая срочная помощь http://www.vishivayu.ukrbb.net/viewtopic.php?f=12&t=13503/ .
What i do not realize is in truth how you’re no longer really much more neatly-favored than you might be now.
You are very intelligent. You recognize therefore significantly in relation to this subject,
produced me in my view imagine it from so many numerous angles.
Its like men and women don’t seem to be involved unless it’s something to do with Girl gaga!
Your individual stuffs nice. All the time care for it up!
Can’t wait to spend more time by the pool now that my screens are fixed thanks to All Screening of SWFL! Pool Cage Screen Repair
http://fastpillseasy.com/# cheapest ed meds
неотложная наркологическая помощь в москве http://www.vishivayu.ukrbb.net/viewtopic.php?f=12&t=13503/ .
This was highly helpful. For more, visit juegos de casino gratis para jugar
загранпаспорт срочно загранпаспорт срочно .
Just had my aircon fastened, and I’m so relieved! If someone’s searching out legitimate amenities in Canoga Park, you may still visit ac repair service for extensive recommendations
viagra without prescription: FastPillsForMen.com – buy Viagra over the counter
I just couldn’t depart your website prior to suggesting that I actually enjoyed the usual info a person supply in your guests?
Is going to be back incessantly to inspect new posts
неотложная наркологическая помощь в москве неотложная наркологическая помощь в москве .
срочное изготовление загранпаспорта срочное изготовление загранпаспорта .
Can’t thank my chiropractor enough for helping me bounce back after my accident! Anyone else had a similar experience? If so Chiropractor Kent, WA
https://fastpillseasy.com/# buy erectile dysfunction treatment
https://maxpillsformen.com/# Buy Cialis online
It’s amazing how chiropractic care can help with pain relief after an accident! I discovered some great resources at Lakewood WA Chiropractor that others might find useful too
вызов нарколога на дом частная скорая помощь вызов нарколога на дом частная скорая помощь .
вызвать наркологическую помощь вызвать наркологическую помощь .
https://fastpillsformen.com/# Generic Viagra online