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
Добро пожаловать на наш ресурс, где вы найдете большое количество полезных статей на тему приемки недвижимости в новостройке или покупки квартиры без отделки. Узнайте, как обезопасить себя от ненадёжных застройщиков, какие проверки осуществить перед покупкой жилья, и о многом другом!
https://citadel-ca.ru
You’ll Be Unable To Guess Dangerous Drugs Lawsuits’s Benefits dangerous Drugs lawsuit
At this moment I am going away to do my breakfast, when having my breakfast coming again to read additional news.
10 Things Everyone Gets Wrong About Starlight Princess demo slot starlight princess rupiah
Are You Getting The Most From Your Free Casino Slots? Evoplay slots compatibility
15 Things You Didn’t Know About Federal Employers fela Lawyers
Responsible For A Slot Zeus 1000 Budget? 10 Ways To Waste Your Money akun demo pragmatic Zeus
Guide To Double Glazing Near Me: The Intermediate Guide For Double Glazing Near Me double glazing near me
This post provides clear idea designed for the new viewers of blogging, that actually how to
do running a blog.
Discovered an article that will definitely interest you – don’t miss the chance to familiarize yourself http://grot.getbb.ru/viewtopic.php?f=6&t=998
aviator game online real money aviator game online real money .
What’s The Job Market For Slots For Fun Professionals
Like? Slots for fun
24 Hours For Improving Personal Injury Lawsuit Personal injury lawyers
5. Replacement Upvc Window Handles Projects For Any Budget upvc window repair near me
The 10 Scariest Things About Double Glazing Near Me Installer
The Complete List Of Best Online Shopping Sites For Clothes Dos And Don’ts Vimeo.Com
It’s wonderful that you are getting ideas from this article as well as from our argument made at this place.
Why You’ll Definitely Want To Learn More About Progressive Jackpot Slots Slots
With Free Bonus Rounds (http://Www.Google.Bs)
The Reasons Rewriter Tool Is Harder Than You Think Word Ai
5 Semi Truck Lawyer Projects For Every Budget semi Truck Accident Attorneys
10 Facts About Lexus Key Fob Price That Will Instantly Set You In A
Positive Mood lexus ls430 key fob replacement
Upvc Windows Near Me Tools To Help You Manage Your Everyday Lifethe Only Upvc Windows Near Me Trick That Everyone
Should Know Upvc Windows Near Me (Kb0B6Iz7Fozpq8Ab7T91Krji.Com)
Thankfulness to my father who told me about this webpage,
this website is genuinely amazing.
This is very attention-grabbing, You’re an overly professional blogger.
I have joined your rss feed and sit up for searching for extra of your excellent post.
Also, I’ve shared your site in my social networks
The 10 Scariest Things About Walking Desk
Treadmill Walking desk Treadmill
비아그라 구입 병원
병원에서의 비아그라: 환자에게 안전하고 효과적인가?
How Top-Rated Mobility Scooters Influenced My Life For The Better superior mobility Scooters
What’s The Job Market For Double Glazing Near Me Professionals?
double Glazing near me
Thanks for your post, I appreciate the hard work you put into it. Do you love online casino games? do you need help how to play? I can definitely help you for free.
https://main7.top
14 Questions You’re Refused To Ask Slot Challenges entertaining slots (Angie)