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
A Complete Guide To 4-Wheel Mobility Scooters 4 wheel scooters
buy cytotec online fast delivery buy misoprostol over the counter purchase cytotec
I love what you guys tend to be up too. This sort of clever work and reporting!
Keep up the excellent works guys I’ve incorporated you guys to our blogroll.
Also visit my webpage :: cancer research
10 Slot Developers Tricks All Experts Recommend casino
slots guide; Lida,
10 Meetups On ADHD Titration Private You Should Attend Adhd Titration Uk London (https://Imoodle.Win/)
This article provides clear idea for the new users of blogging, that
actually how to do blogging and site-building.
Check out my web site :: 해외선물 대여업체
tamoxifen for sale tamoxifen premenopausal alternative to tamoxifen
15 Secretly Funny People In Locksmith Car Near Me car key Locksmith near Me
https://lisinopril.network/# lisinopril 2.5 cost
https://cytotec.club/# cytotec buy online usa
buying propecia buying cheap propecia no prescription cheap propecia without dr prescription
What’s The Current Job Market For Private Psychiatrist Birmingham Professionals?
private psychiatrist birmingham
москва фулфилмент вайлдберриз https://24fulfilment-marketplace.ru/
Are You Tired Of Double Glazing In Leeds? 10 Inspirational Ideas
To Revive Your Love For Double Glazing In Leeds Double Glazed Doors Repairs
How To Explain Four Wheeled Scooters To A Five-Year-Old Electric 4 Wheel Mobility scooter
nolvadex online: tamoxifen rash – tamoxifen and grapefruit
http://lisinopril.network/# zestril medicine
effexor and tamoxifen: tamoxifen hip pain – tamoxifen hormone therapy
I truly love your blog.. Very nice colors & theme. Did you
create this web site yourself? Please reply back as I’m planning to
create my very own blog and want to learn where you got this from
or exactly what the theme is named. Thanks!
The Top Reasons People Succeed Within The Ghost Immobiliser Installers Near Me Industry
ghost immobiliser installers near Me
Can Cerebral Palsy Claim Be The Next Supreme Ruler Of The
World? Cerebral palsy law firms
fantastic submit, very informative. I wonder why the opposite specialists
of this sector don’t notice this. You should proceed your writing.
I’m sure, you’ve a great readers’ base already!
The 10 Scariest Things About Double Glazing Company Near
Me Double Glazing Company Near Me
Abortion pills online: purchase cytotec – buy cytotec over the counter
фулфилмент платформа https://24fulfilment-marketplace.ru/
I don’t even know how I ended up here, but I thought this post was good.
I do not know who you are but definitely you are going to a
famous blogger if you are not already 😉 Cheers!
how to buy prednisone
The Best Vehicle Diagnostics It’s What Gurus Do Three Things Mobile Diagnostic Near Me
http://lisinopril.network/# lisinopril tabs 10mg
вайлдберриз фулфилмент https://24fulfilment-marketplace.ru/