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
Hey there, You have done a great job. I’ll definitely digg it and personally suggest to my friends.
I’m sure they will be benefited from this site.
https://rybelsus.shop/# semaglutide online
This Is The Kids Bunk Bed Case Study You’ll Never Forget Bunk Bed For Kids
11 “Faux Pas” Which Are Actually OK To Make With Your Upvc Doors Luton Windows and Doors Near me
Your Worst Nightmare Concerning Retro Fridge Freezers Cream Get Real under counter retro fridge freezer,
https://www.google.pt,
You’ll Never Be Able To Figure Out This Best G Spot Toy’s Benefits best g spot Toy
It’s The One Affordable SEO London Trick Every Person Should Learn Search Engine Optimization London
Услуги Riga escort предлагают идеальное сопровождение для вашего отдыха или деловой поездки https://www.escortinriga.lv
You actually make it seem so easy with your presentation but I find this topic to be actually
something which I think I would never understand.
It seems too complicated and very broad for me. I am looking forward
for your next post, I’ll try to get the hang of it!
5 Killer Quora Answers To Private ADHD Assessment UK Private Adhd Assessment Uk
Hi to every body, it’s my first go to see of this web
site; this web site carries amazing and actually
good material designed for readers.
It’s a shame you don’t have a donate button! I’d definitely donate to this
excellent blog! I suppose for now i’ll settle for bookmarking
and adding your RSS feed to my Google account. I look forward to new updates and will share this website with my Facebook group.
Chat soon!
semaglutide cost cheapest rybelsus pills semaglutide online
15 Best Documentaries About Double-Ended Dildos Female Double Dildo
7 Useful Tips For Making The Most Of Your Electric Folding Lightweight Wheelchair folding lightweight electric wheelchair – mobility-scooter85287.wikienlightenment.com,
Find Out What Freestanding Bioethanol Fireplace The Celebs Are Using bio-ethanol fireplace (Maya)
See What Cheapest Automatic Folding Mobility Scooter Tricks The Celebs Are Utilizing cheapest automatic folding mobility scooter (Charles)
14 Smart Ways To Spend Your Leftover Bristol Windows And Doors Budget upvc window and door repairs
https://ozempic.art/# buy cheap ozempic
semaglutide online semaglutide cost semaglutide cost
I do agree with all of the concepts you’ve presented for your
post. They’re very convincing and will certainly work. Nonetheless,
the posts are very quick for newbies. May just
you please prolong them a little from subsequent time?
Thanks for the post.
Five Killer Quora Answers To Bio Ethanol Fireplaces ethanol fireplace (Darrell)
9 Things Your Parents Teach You About Stroller 2 In 1 Car Seat Stroller 2 in 1 Car seat; olderworkers.Com.au,
http://rybelsus.shop/# cheapest rybelsus pills
15 Things You Didn’t Know About Best French Door Fridge Freezer Uk French Door Retro Fridge
http://rybelsus.shop/# rybelsus coupon
This Week’s Top Stories About Wall-Mounted Fireplace Wall-Mounted Fireplace Wall mounted electric Fires uk (whitebookmarks.com)
Search Engine Optimization Basics For Beginners – Learn Search
Engine Seduction 백링크 구매
What’s up to every , as I am truly keen of reading this weblog’s post to be updated
regularly. It consists of good stuff.
Wow, this article is fastidious, my sister is analyzing
such things, so I am going to convey her.