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
Take a look at my web site :: toko bunga Ungaran murah
продвижение сайтов цена в москве https://prodvizhenie-sajtov-v-moskve115.ru/ .
Mutuo 22 mila euro
Harry Kane https://harry-kane.prostoprosport-br.com recebeu um convite para a selecao sub-alterna da Inglaterra pela primeira vez tempo 17 para o torneio juvenil em Portugal. Ao mesmo tempo, o atacante, devido a doenca grave, nao compareceu ao triunfante Campeonato Europeu Sub-17 masculino de 2010 pelos britanicos.
how to get generic tetracycline no prescription
Awesome article.
Take A Look At Your Fellow Workers Compensation Litigation Enthusiasts.
Steve Jobs Of The Workers Compensation Litigation Industry
workers’ compensation
where to buy cheap ashwagandha pill
Responsible For An Filter Coffee Budget? 10 Incredible
Ways To Spend Your Money drip pot Coffee maker – Blog.Soboku.jp –
naturally like your web-site however you need to take a look
at the spelling on several of your posts.
Several of them are rife with spelling issues and
I find it very bothersome to tell the truth on the other hand I will certainly come
again again.
The 10 Scariest Things About List Of Online Shopping Sites Uk list of online shopping Sites uk
Drugs information for patients. Generic Name.
where buy digoxin price
All about medicine. Read information here.
5 Killer Quora Answers On Secondary Double Glazing Near Me
double glazing near Me
How To Recognize The Asbestos Attorneys
Which Is Right For You http://www.cassylawn.top
I’ve been browsing online more than 2 hours today, yet I never found any
interesting article like yours. It is pretty worth enough for me.
In my opinion, if all site owners and bloggers made good content
as you did, the web will be a lot more useful than ever
before.
14 Clever Ways To Spend On Leftover Nespresso Krups Machine Budget nespresso Capsule machine
seo продвижение корпоративных сайтов в москве http://www.prodvizhenie-sajtov-v-moskve115.ru/ .
Wonderful beat ! I wish to apprentice whilst you amend your website, how can i
subscribe for a blog site? The account helped me a applicable deal.
I had been a little bit acquainted of this your broadcast offered vibrant
transparent concept
Door Fitting Bedford Tools To Simplify Your Everyday Life french doors Bedfordshire
20 Questions You Should Be Asking About Personal Injury Claim Before Purchasing It personal injury Law Firm
The Door Repair Near Me Awards: The Best, Worst And Strangest Things
We’ve Ever Seen window Replacement near Me
Are you looking for a solution to erectile dysfunction? Look no further! buy viagra australia online is here to help.
can you buy generic atarax tablets
The Good And Bad About Slot Tips real casino slots (migration-bt4.co.uk)
5 Laws Everyone Working In Shopping Online Should Be Aware Of Shopping online sites Clothes – my.advantech.Com,
11 Methods To Completely Defeat Your Vegas Slots Penny Slots
10 No-Fuss Ways To Figuring The Free Slots You’re Looking For bonus Round Slots
10 Things That Your Family Teach You About Private Psychiatric Assessment
psychiatric assessment near me (Alphonso)
Medication information sheet. What side effects?
where can i get fluoxetine tablets
Everything trends of medicine. Get information here.
I need to to thank you for this good read!! I certainly
enjoyed every bit of it. I have got you saved as a
favorite to look at new stuff you post…