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
Ahaa, its pleasant conversation about this article at this place at this webpage, I
have read all that, so at this time me also commenting at this place.
best mail order pharmacy canada: Canadian Md Pharm – vipps canadian pharmacy
hello!,I love your writing very much! share we keep up a
correspondence extra about your article on AOL? I need a specialist in this area
to solve my problem. Maybe that is you! Taking a look forward to see
you.
I’ve seen what clogged gutters can do to houses around here Gutter Cleaning Amherst
mexico pharmacies prescription drugs: medication from mexico pharmacy – mexican drugstore online
Mexican Easy Pharm: Mexican Easy Pharm – mexican border pharmacies shipping to usa
Hello! I know this is kind of off topic but I was wondering
which blog platform are you using for this website?
I’m getting sick and tired of WordPress because I’ve had problems with hackers
and I’m looking at alternatives for another platform.
I would be awesome if you could point me in the direction of a good platform.
canada cloud pharmacy: canadian pharmacy prices – canadian pharmacy meds
Great website! It looks extremely professional!
Keep up the helpful job!
https://infoskierniewice.pl
canadian drugs online: canadian pharmacy ed medications – canadapharmacyonline legit
Much thanks! It is definitely an incredible web page!
https://gostyn24.pl
диплом педагога купить
This is very insightful. Check out بلفاروپلاستی چیست for more
https://indiancertpharm.com/# Indian Cert Pharm
my canadian pharmacy
Wow, stunning website. Thnx …
https://www.tvsudecka.pl
Oh mmy goodness! Amazing article dude! Ⅿany tһanks, Howevеr Ι am experiencing issues ѡith yߋur RSS.
I don’t know the reason wһy I am unable to sybscribe to іt.
Is thwre anyboɗy else getting sіmilar RSS problems? Anyone ԝho knows the answer caan ʏⲟu kindly respond?
Tһanks!!
my page … đồng hồ nữ đẹp chính hãng
mexican pharmaceuticals online: mexico drug stores pharmacies – Mexican Easy Pharm
Thanks in support of sharing such a good idea, paragraph is pleasant,
thats why i have read it completely
Passive income through rental properties is one of the most appealing aspects of real estate investing sell my house fast austin
The No. 1 Question That Anyone Working In Grey Fabric
Three Seater Sofa Must Know How To Answer 3 seater grey fabric Sofa
Buy Cocaine Canada buy cocaine canada
Indian Cert Pharm: Indian pharmacy that ships to usa – Online pharmacy
Thank you so much! This is definitely an fantastic webpage!
https://www.gazetamiedzyszkolna.pl
You’re an extremely valuable internet site; could not make it without ya!
https://swiecie24.pl/pl/
выведение из запоя в стационаре выведение из запоя в стационаре .
вывод из запоя в стационаре вывод из запоя в стационаре .
IndianCertPharm: Indian pharmacy that ships to usa – Online medicine
Indian Cert Pharm: Indian Cert Pharm – Indian Cert Pharm
вывод из запоя в нижнем новгороде вывод из запоя в нижнем новгороде .
11 Ways To Completely Redesign Your Pragmatic Official Website 프라그마틱 환수율