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
How Buffalo Mesothelioma Attorney Was The Most Talked About
Trend In 2023 Mesothelioma Attorneys Construction Worker (Annunciogratis.Net)
reputable mexican pharmacies online: mexican pharmaceuticals online – mexico drug stores pharmacies
best online pharmacies in mexico: buying from online mexican pharmacy – best online pharmacies in mexico
Used Folding Mobility Scooter Strategies That Will Change Your Life Mobility scooter folding lightweight
mexican mail order pharmacies: reputable mexican pharmacies online – п»їbest mexican online pharmacies
What’s The Current Job Market For Best Couple Sex Toys Professionals Like?
couple sex toy
Good day! I simply would like to give you a huge thumbs up for
your great info you’ve got here on this post.
I will be returning to your website for more soon.
7 Simple Tricks To Moving Your Anxiety Treatment Medicines natural treatment
for stress and anxiety (bossen-Newman-2.technetbloggers.de)
Appreciating the time and effort you put
into your blog and detailed information you provide. It’s good to
come across a blog every once in a while that isn’t the
same unwanted rehashed information. Wonderful read!
I’ve saved your site and I’m including your RSS feeds to my Google account.
купить диплом в алексине купить диплом в алексине .
https://drbo2b.z8.web.core.windows.net/research/drbo2B-(453).html
The mother of the bride and mother of the groom normally put on fully different dress styles and colours.
all the time i used to read smaller posts which also
clear their motive, and that is also happening with this
piece of writing which I am reading at this time.
Guide To Upvc Repairs Near Me: The Intermediate Guide Towards Upvc Repairs
Near Me upvc repairs near me – http://www.plantsg.com.Sg –
Please let me know if you’re looking for a author for your site.
You have some really good posts and I feel I would be a good asset.
If you ever want to take some of the load off, I’d absolutely love
to write some content for your blog in exchange for a link back
to mine. Please shoot me an email if interested. Thanks!
уборка ванной комнаты и туалета цена
pharmacies in mexico that ship to usa: purple pharmacy mexico price list – buying prescription drugs in mexico
The other day, while I was at work, my sister stole my iPad and tested to
see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My
iPad is now destroyed and she has 83 views. I know this is
totally off topic but I had to share it with someone!
Admiring the time and energy you put into your website
and detailed information you present. It’s good to
come across a blog every once in a while that
isn’t the same outdated rehashed information. Excellent read!
I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.
https://digi7b.netlify.app/research/digi7b-(12)
You will wish to find something that works in each cold and hot, or find one thing to keep you heat to go over it.
Fridge Freezer Cheap Tools To Streamline Your Daily Lifethe
One Fridge Freezer Cheap Trick That Should Be Used By Everyone Be Able To Fridge Freezer Cheap
I couldn’t refrain from commenting. Perfectly written!
purple pharmacy mexico price list: medicine in mexico pharmacies – mexican mail order pharmacies
https://accounting2.research.au-syd1.upcloudobjects.com/research/accounting2-(322).html
This consists of most variations of white, similar to ivory and champagne.
The 10 Most Terrifying Things About Senseo Philips Coffee Maker senseo philips coffee maker (https://www.diggerslist.com/66941Ede468df/about)
mexican rx online: best online pharmacies in mexico – reputable mexican pharmacies online
How To Explain Lightweight Folding Wheelchair To Your Grandparents Lightweight Foldable Wheelchair (Olderworkers.Com.Au)
Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you! By the way, how could we communicate?
pharmacies in mexico that ship to usa: medicine in mexico pharmacies – purple pharmacy mexico price list
buying prescription drugs in mexico: mexico drug stores pharmacies – mexican rx online
You’ll Never Be Able To Figure Out This Upvc Window Doctor’s Tricks
upvc window doctor [Historydb.Date]