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
Upvc Windows Near Me Tools To Ease Your Daily Lifethe One Upvc
Windows Near Me Trick Every Person Should Know upvc Windows near Me
How To Find The Perfect Testing For ADHD In Adults On The Internet adhd Online Testing
You’ll Never Guess This Top UK Pornstars’s Tricks top uk pornstars
How To Explain Upvc Window Repairs To Your
Grandparents upvc window Repairs near me
7 Simple Secrets To Totally Rocking Your Secure Slots online Slots
7 Small Changes That Will Make An Enormous Difference To Your
Upvc Windows Repairs window Repair
Nine Things That Your Parent Teach You About Cost Of
Replacement Car Key Replacement car key cost
http://ciprofloxacin.tech/# cipro for sale
antibiotics cipro ciprofloxacin order online buy ciprofloxacin
cost propecia: buy generic propecia pill – buying propecia price
tamoxifen reviews tamoxifen citrate where to get nolvadex
10 Undisputed Reasons People Hate Mesothelioma Claim
Asbestos Attorney
LГҐn 40000 kr
Need Inspiration? Check Out Vauxhall Astra Replacement Key Cost Vauxhall adam key
http://nolvadex.life/# aromatase inhibitor tamoxifen
of course like your website however you have to test the spelling on quite a few of your posts.
Several of them are rife with spelling issues
and I to find it very bothersome to inform the reality however
I will definitely come again again.
10 Reasons Why People Hate CS GO Case Battle. CS
GO Case Battle revolver case (Candra)
Why viewers still use to read news papers when in this technological globe all is accessible on web?
buy cytotec: Cytotec 200mcg price – buy cytotec over the counter
Renault Trafic Key Replacement Tips From The Best In The Industry Renault key
Don’t Make This Silly Mistake With Your Repair Upvc Window upvc Window Repairs
14 Smart Strategies To Spend Left-Over Private ADHD Clinic
Budget adult Adhd private assessment
The No. Question Everybody Working In What CSGO Cases Have
Butterfly Knives Must Know How To Answer Gamma Case
The Most Advanced Guide To CSGO Cases Highest Roi operation bravo case
Cytotec 200mcg price buy cytotec pills cytotec online
lisinopril tab 5 mg price: 3 lisinopril – lisinopril 2.5 mg buy online
cytotec abortion pill: buy cytotec in usa – buy cytotec over the counter
20 Fun Facts About Cheap 9kg Washing Machines 9kg washing machines – http://extension.unimagdalena.edu.co/,
http://nolvadex.life/# does tamoxifen make you tired
cipro for sale buy generic ciprofloxacin cipro pharmacy