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
pharmacies in mexico that ship to usa: mexico drug stores pharmacies – xxl mexican pharm
darknet market https://github.com/darknetmarketslinks/darknetmarketlinks – dark web search engines tor dark web
Allow me show you in my testimonial of NewULife.
Review my web site – cleaning services calgary
canadian pharmacy coupon code https://familypharmacy.company/# Online pharmacy USA
30 Inspirational Quotes About Pallet Wood For Sale Pallet buying
canadian pharmacy without prescription http://easycanadianpharm.com/# easy canadian pharm
Low Budget Travel Tips 부산
I like it when individuals come together and share
opinions. Great website, stick with it!
Buy Cocaine Canada buy cocaine canada
dark market list https://github.com/darkwebmarketslinks/darkwebmarkets – deep dark web dark web market
easy canadian pharm: canada pharmacy reviews – canadian pharmacies compare
dark market onion https://github.com/darknetwebsitesgflpx/darknetwebsites – dark market 2022
Всем привет! Подскажите, где найтиразные статьи о займах? Сейчас читаю – allforb.ru
darknet drug market https://github.com/darkwebmarketslinks/darkwebmarkets – tor darknet darknet seiten
NewULife Ranks. Welcome to my NewUlife Review.
Here is my web blog; is epdm roofing any good
Apkslot adalah situs games apk slot online paling banyak di
download oleh masyarakat karena memiliki pelayanan profesional serta bonus besar untuk
seluruh member, tersedia banyak pilihan permainan dengan grafik bagus
memuaskan
I have been surfing online more than three hours as
of late, but I never found any fascinating article like
yours. It is lovely price enough for me. In my opinion, if all web owners and bloggers made
good content as you did, the net can be much more helpful
than ever before.
Emergency Treatment Training Brisbane is essential for everyone! I highly recommend having a look at the training courses provided at First Aid Courses Brisbane CBD
NewULife Ranks. Welcome to my NewUlife Review.
Also visit my webpage … vector animation software
best online pharmacy no prescription https://megaindiapharm.shop/# Mega India Pharm
mexico drug stores pharmacies: purple pharmacy mexico price list – xxl mexican pharm
exist also concerns about the efficacy and safety of some https://martabak88slot.net/how-much-does-stem-cell-therapy-cost-understanding-3-2/, especially those that but they have
not {{been|turned into} thoroughly tested in clinical trials.
easy canadian pharm easy canadian pharm easy canadian pharm
xxl mexican pharm: medication from mexico pharmacy – mexican mail order pharmacies
10 Things You’ve Learned In Preschool That Can Help You In Psychiatric Assessment psychiatric assessment glasgow
It is the best time to make some plans for the longer
term and it is time to be happy. I’ve learn this publish and if I could I want to counsel you some attention-grabbing issues or suggestions.
Perhaps you can write subsequent articles referring to this article.
I desire to learn even more things about it!
dark markets https://github.com/darkwebmarketslinks/darkwebmarkets – darknet search engine darknet seiten
deep web drug links https://github.com/darknetdruglinksvojns/darknetdruglinks – tor market links
NewULife Ranks. Invite to my NewUlife Review.
my blog post – cleaning services calgary
You really make it seem so easy with your presentation but I find this
matter to be really something which I think I would never understand.
It seems too complicated and very broad for me. I’m looking forward for your next
post, I’ll try to get the hang of it!