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
L Shaped Kids Beds Tools To Streamline Your Daily Lifethe One L
Shaped Kids Beds Trick Every Individual Should Learn l shaped kids bed
List Of Online Shopping Sites Uk: 11 Thing You’re Forgetting To Do Jolie Papier
Online Shop Uk Amazon (http://Www.Sellere.De)
17 Signs You’re Working With Motorcycle Accident Legal motorcycle accident lawyers
Your Family Will Be Thankful For Getting
This Titration Process titration period Adhd
5 Killer Quora Answers To Slot Experiences slot experiences
can i order valtrex without prescription
Unexpected Business Strategies Helped Americanfridge Freezer Succeed zackfoxworth.top
Meet Your Fellow Mesothelioma Law Firms Enthusiasts.
Steve Jobs Of The Mesothelioma Law Firms Industry Mesothelioma Claims
16 Facebook Pages That You Must Follow For Double Glazing Repairs Near Me Marketers http://www.jerealas.top
This Is The Advanced Guide To Window Glass Replacement Near Me Cheapest
Thanks designed for sharing such a nice
thought, post is good, thats why i have read it fully
15 Top Birth Defect Compensation Bloggers You Should
Follow Birth defect Attorney
10 Facts About Upvc Window Repairs That Make You Feel Instantly The Best Mood upvc window repairs Near me
Auto Accident Case Tools To Make Your Everyday Lifethe Only Auto Accident Case Trick Every Individual Should Learn auto
accident (Edison)
where can i get generic celexa without a prescription
Peculiar article, just what I needed.
Simply desire to say your article is as astounding.
The clearness in your post is just cool and i could assume you’re an expert
on this subject. Fine with your permission let me to grab your
feed to keep updated with forthcoming post. Thanks a million and please continue the gratifying work.
Heⅼlo thеre, I believe yoir web site could be having web browser compatibiloity рroblems.
Ꮃhen I l᧐ok aat your web site іn Safari,
it lоoks fine һowever, ѡhen ⲟpening іn ΙE, іt hɑs some overlaoping issues.
I meгely wаnted tо giѵe yoᥙ ɑ quick heads սр! Ⲟther than that,
ɡreat blog!
Aⅼso visit my web site :: situs judi slot
Enough Already! 15 Things About ADHD In Adults Test We’re Sick Of Hearing
adhd testing in adults, Kacey,
10 Of The Top Facebook Pages Of All Time About Online Shop Best Luxury Online Shopping Sites Uk
A loan provides you the funds you wan upfront and lets you spread the
cost of paying it back.
my web page :: Dorthy
Sweet blog! I found it while browsing on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem
to get there! Thanks
can i get cheap tizanidine without dr prescription
cefixime 100mg cap
From my point of view, It’s worth the price as a comparison to the worth we get.
Look into my website; instant payday loans – mediawiki.volunteersguild.org,
Guide To Online Shopping Uk Discount: The Intermediate
Guide The Steps To Online Shopping Uk Discount online shopping uk
(toolbarqueries.google.mk)
10 Things That Everyone Doesn’t Get Right About Upvc Door Mechanism
replacement Door panels upvc (p3terx.com)
The EPA prohibits the manufacturing processing,
importation, and distribution of most asbestos-containing products.
However, asbestos-related lawsuits continue to appear on the court dockets.
Drugs information for patients. Long-Term Effects.
can i get cheap motrin no prescription
Best about drug. Get here.
Best Online Shopping Sites For Clothes Tools To Help You Manage
Your Daily Lifethe One Best Online Shopping Sites For Clothes
Trick That Every Person Should Be Able To Best Online Shopping Sites For Clothes