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
14 Questions You Might Be Afraid To Ask About Mental Assessment Mental health Assessment depression
What’s The Job Market For Best Online Shopping Groceries Uk Professionals Like?
best online shopping groceries uk; chudacek.blog.idnes.cz,
buy lyrica online
how to get avodart without prescription
You’ll Never Be Able To Figure Out This 18 Wheeler Accident
Law Firm’s Benefits 18 Wheeler Accident Law Firm
How Mental Health Assessment Became The Hottest Trend Of
2023 72 hour Assessment mental health (https://tujuan.grogol.Us)
My brother suggested I might like this web site. He was totally right.
This post truly made my day. You cann’t imagine just how much time I had spent for this info!
Thanks!
Genuine HGH Benefits (NewULife Hgh Gel Not Checked Yet).
my webpage: Bookmarks
How To Build Successful Door Repairs Near Me
How-Tos And Tutorials To Create Successful Door Repairs Near Me Home upvc door repairs near me
Svako ima misljenje o automobilima, ali komentari su nepresusan izvor ideja.
Citroen Corvette je san svakog ljubitelja brzine.
Voziti moj auto je kao hodanje po minskom polju.
оборудование конференц зала оборудование конференц зала .
конференц зал оборудование конференц зал оборудование .
современное оборудование для конференц зала современное оборудование для конференц зала .
where can i get cheap finpecia online
how to buy generic accutane without prescription
I blog quite often and I really appreciate your information. The article
has truly peaked my interest. I will bookmark
your site and keep checking for new information about once a
week. I opted in for your Feed too.
The intelligence is the most effective completely dry natural
herb vape I have actually tried.
Also visit my blog – what beers are vegan uk
The Top Reasons People Succeed Within The Workers Compensation Compensation Industry workers’ compensation lawsuit
How To Resolve Issues With Car Accident Lawyer car accident law firms
оборудование для конференц зала готовые решения https://www.oborudovanie-konferenc-zalov11.ru .
where to buy generic allopurinol pill
get generic imuran without a prescription
Ederson Santana de Moraes https://edersonmoraes.prostoprosport-br.com Futebolista brasileiro, goleiro do clube Manchester City e da Selecao Brasileira . Participante do Campeonato Mundial 2018. Bicampeao de Portugal pelo Benfica e pentacampeao de Inglaterra pelo Manchester City.
The Best Place To Research Semi Truck Claim Online semi Truck accident lawyer
cost generic caduet pills
A synthetic USP human development hormone (somatropin).
Here is my web-site :: type 1 diabetes disability benefits uk
Are Mini Nespresso Machine The Greatest Thing There Ever Was?
White Nespresso Coffee Machine (https://83.Viromin.Com)
оснащение конференц залов оснащение конференц залов .
The IQ is vape juice vegan the very
best completely dry natural herb vape I have actually tried.
оборудование для конференц залов оборудование для конференц залов .