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
Incredible! This blog looks exactly like my old one!
It’s on a completely different subject but it has pretty much the same layout and design.
Outstanding choice of colors!
What’s The Job Market For Replacement Bmw Key Professionals Like?
replacement bmw Key
Just moved with the help of moving company queens
Hі colleagues, nicce ppost annd goodd aгgumkents commented here, I amm trujly enjoying bby these.
Loook ito mmy homepage … “Free Gift”
deep web sites deep web markets tor markets links
Incredible! This blog looks just like my old one!
It’s on a completely different subject but it has pretty much the same layout and design. Excellent choice of colors!
Look at my page; เครดิตฟรีไม่มีเงื่อนไข
Kudos to all the hard-working movers helping families relocate; it’s no small task! Make sure you connect with ### anykeyword ### when considering your next move from long distance movers sarasota
Local moves can be challenging; that’s why I turn to #_## anykeyword###_ every time I need help moving company brooklyn
What an empowering experience it was attending the First Aid Course in Adelaide—I feel ready for anything now! CPR course
I’m curious about pricing for long distance movers in Sarasota—has anyone checked out cross country movers
Excellent post. I was checking constantly this blog and I am impressed!
Extremely helpful info particularly the last part 🙂 I care
for such information a lot. I was looking for this certain info for a long time.
Thank you and best of luck.
Some times its a pain in the ass to read what people wrote but this web site is really user genial!
Nice post. I learn something new and challenging on websites I stumbleupon everyday.
It’s always helpful to read articles from other authors and
practice something from other websites.
Here is my web blog contact lens
Hello, of course this post is actually fastidious and I have learned lot of things
from it about blogging. thanks.
Moving doesn’t need to be chaotic; just reach out to ####ANYKEYWORD### movers
If some one wishes to be updated with most up-to-date
technologies therefore he must be visit this website
and be up to date all the time.
Medicines prescribing information. Effects of Drug Abuse.
where can i buy ceftin pill
Actual about medicament. Read information now.
buy cipro online usa: CiPharmDelivery – buy ciprofloxacin over the counter
Always check reviews before hiring a moving services —it saved me from a bad
amoxicillin canada price: amoxicillin 500mg without prescription – how much is amoxicillin prescription
Anyone else find packing up sentimental items difficult during their last big move? Let’s talk feelings over at long distance mover
I’m thrilled that I chose reliablelocalmoversformyrecentmoveinStPetersburg; explorevaluableinformationaboutfindingmoversat### anykeyword### local moving companies st petersburg
If you’re relocating soon long distance movers
Shifting homes is tough but having great movers like ###ANYKEYWORD### makes such a difference! moving company brooklyn
I love the idea of DIY data recovery—it’s empowering! I’ve bookmarked your page, and I’m also exploring other resources like mac data recovery
Покупка школьного аттестата с упрощенной программой: что важно знать
https://prednibest.com/# prednisone 5 mg tablet without a prescription
Jugabet Jugabet .
I like the helpful info you provide in your articles.
I’ll bookmark your blog and check again here regularly.
I’m quite certain I will learn many new stuff right here!
Good luck for the next!
dark websites darknet drugs dark web site