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
Узнай все о удлинение полового члена москва увеличение длины полового члена
Great reminders about the impact of weather on roofs—I’ll be scheduling regular inspections from now on! roofing contractors vancouver
cost of cheap celebrex no prescription
excellent issues altogether, you just received a new
reader. What would you recommend about your publish that
you made a few days in the past? Any sure?
what is minocycline prescribed for buy Stromectol minocycline 50mg without a doctor
cytotec pills buy online: cyt premium – cytotec abortion pill
plavix best price PlavixClo Best Price buy clopidogrel online
You actually said it terrifically.
I got this web page from my friend who told me concerning this web site and at the moment this time I am visiting this web site and
reading very informative articles or reviews at this time.
amoxicillin where to get: com pharm – how to buy amoxycillin
For the best pressure cleaning service in Conway, I always advise house cleaning services conway ar
I am genuinely grateful to the owner of this website who has shared this fantastic
piece of writing at at this place.
Are You Getting The Most Value Of Your Best Male Masturbators?
best male mastrabator (Kristopher)
It’s hard to come by well-informed people on this
subject, however, you seem like you know what you’re talking
about! Thanks
my website – คาสิโนauto
where to buy prednisone in australia: ray pharm – 50mg prednisone tablet
Fantastic advice on using video content for law firms! It’s a great way to improve SEO and engage potential clients seo for legal firm
Узнай все о хирургическое увеличение члена операция по увеличению члена
Пошаговая инструкция по официальной покупке диплома о высшем образовании
buy cheap plavix price
Купить диплом экономиста – оптимальное решение
Купить диплом Барнаул
If you desire to get a good deal from this post
then you have to apply such strategies to your won website.
cost generic clomid for sale: clomid – get generic clomid pills
Купить диплом судоводителя
Plavix 75 mg price: Clopidogrel Best Prices – buy Clopidogrel over the counter
Excellent blog post. I certainly love this site.
Stick with it!
кухни на заказ – сочетание оригинального дизайна и практичности.
Узнай все о удлинение полового члена цена увеличить член цена
id=”firstHeading” class=”firstHeading mw-first-heading”>Search rеsults
Ꮋelp
English
Tools
Tools
mоᴠe to sidebar hide
Actons
Ԍeneral
Aⅼѕo visit mү web-site พวงหรีด งานศพ
buy Lisinopril 1st: cheapest Lisinopril – buy Lisinopril online