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
Medicine prescribing information. Long-Term Effects.
how can i get cheap dramamine without prescription
Actual news about drugs. Read information here.
Seasonal rhinitis treatment
10 Top Mobile Apps For Assessment Of Adult Adhd Gp Adhd Assessment
http://alt1.toolbarqueries.google.bf/url?q=https://medmens.ru
Ten Stereotypes About Veterans Disability Lawyers That Don’t Always Hold
vimeo.Com
Hey there, I think your website might be having
browser compatibility issues. When I look at
your blog in Chrome, it looks fine but when opening in Internet Explorer,
it has some overlapping. I just wanted to give you a quick
heads up! Other then that, very good blog!
What Is Machine Coffee And How To Make Use Of
It http://www.4182051.xyz
8 Tips For Boosting Your Treadmill Incline Foldable Game zackfoxworth.top
This is very attention-grabbing, You’re an excessively professional blogger.
I have joined your rss feed and stay up for in search of
extra of your wonderful post. Also, I’ve shared your
site in my social networks
An outstanding share! I’ve jᥙst forwarded this ⲟnto a colleague ѡho haԀ been conducting ɑ littlе homework ⲟn tһis.
And he actually оrdered me dinner simply ƅecause I discovered іt fⲟr him…
lol. Sօ allow mme to reword this…. Thank YOU for the meal!!
But yeah, thanbx fоr spending timе to talk
ahout tһis subject here ߋn yоur blog.
Also visit my page … Gopek178 Hoki
10 Amazing Graphics About Mesothelioma Lawsuit Mesothelioma Law Firms; http://Www.Google.Ru,
10 No-Fuss Ways To Figuring Out Your 18 Wheeler Truck Accident Attorney 18 Wheeler Accident Lawsuit
9 Things Your Parents Teach You About Upvc Window Repair
Near Me Window Repair Near Me
Drug prescribing information. Drug Class.
how to get levitra tablets
Everything information about meds. Get information now.
You could definitely see your expertise in the work you write.
The world hopes for more passionate writers such as you who aren’t afraid to say how they believe.
All the time follow your heart.
Ten Upvc Doors That Really Improve Your Life Upvc doors
The 12 Types Of Twitter Fireplace The Twitter Accounts That You
Follow Lynn Bolvin
11 Ways To Totally Defy Your Windows And Doors Birmingham birmingham glaziers [Delmar]
jasminer
What’s The Current Job Market For Double Glazed Window
Repairs Professionals Like? double glazed window repairs (inbest.clickcorp.kr)
capcut premium latest version,capcut 2022 mod apk,apk capcut pro premium,capcut app download premium unlocked,capcut pro com,capcut pro download,capcut app download apk mod, https://icapcut.com capcut mod apk 2022 download,cat cut apk premium,capcut video editor pro mod apk,capcut pro for windows,
5 Reasons To Consider Being An Online Motor Vehicle Lawyers Buyer And
5 Reasons Why You Shouldn’t motor vehicle accident attorneys
(http://www.google.com)
The Comprehensive Guide To Undiagnosed ADHD In Adults Adhd Diagnosed In Adulthood
Pills information for patients. Long-Term Effects.
get lopressor prices
Actual trends of meds. Get information here.
15 Twitter Accounts You Should Follow To Learn More About Railroad
Injuries Attorneys railroad injuries lawsuits
They have items to offer or solutions to offer.
Feel free to surf to my web page ai headshot generator reddit
Excellent goods from you, man. I’ve understand your stuff previous
to and you’re just extremely magnificent. I really like what you have acquired here, really like what you’re stating and
the way in which you say it. You make it entertaining and you still care for to keep
it smart. I cant wait to read far more from you. This is really a terrific web site.
Medicines information sheet. Effects of Drug Abuse.
can you get esomeprazole price
Best news about medicine. Get information here.
My partner and I absolutely love your blog and find the
majority of your post’s to be exactly what I’m looking for.
can you offer guest writers to write content
available for you? I wouldn’t mind creating a post or elaborating on many of the subjects you write related to here.
Again, awesome website!
I have been surfing on-line greater than 3 hours today, yet I
by no means found any attention-grabbing article like yours.
It is pretty price sufficient for me. In my opinion, if all web owners and bloggers made just right content as you probably did, the net will probably be much more helpful than ever before.