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
The Top Reasons Why People Succeed In The Mesothelioma Lawsuit Industry mesothelioma claims (http://www.nongdui.com/home/link.php?url=https://telegra.ph/14-cartoons-about-vancouver-mesothelioma-lawyer-which-will-brighten-your-day-06-17)
15 Incredible Stats About Workers Compensation Legal Workers’ compensation lawsuits
You’ll Be Unable To Guess Winning Slots’s Tricks
winning slots
You’ll Never Guess This Online Clothes Shopping Websites Uk’s
Tricks Online Clothes Shopping Websites Uk
Childrens Bunk Beds Tools To Improve Your Daily Life Childrens Bunk Beds Technique Every Person Needs To Learn Childrens Bunk Bed
9 Things Your Parents Teach You About Slot Sites Slot Sites
The 10 Scariest Things About Blown Double Glazing Repairs Near
Me double glazing repairs Near me
인도 비아그라
인도의 비아그라: 사용을 둘러싼 법적 및 윤리적 고려 사항
Howdy this is kinda of off topic but I was wondering if
blogs use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding expertise so I wanted to get guidance
from someone with experience. Any help would be greatly appreciated!
Quality articles is the secret to be a focus for the users to pay a visit the web page,
that’s what this site is providing.
You’ll Be Unable To Guess Online Shopping Sites
Top 7’s Tricks Online Shopping Sites Top 7
The Evolution Of Car Boot Scooter arlennizo
An Asbestos case lawyer can assist victims file a lawsuit against their employers
for exposure to the harmful mineral. A lawsuit could help
victims obtain the cost of medical bills in the past, lost wages, and pain and suffering.
Guide To Online Shopping Uk Discount: The Intermediate Guide The Steps To Online Shopping Uk Discount Online Shopping Uk Discount
9 Things Your Parents Taught You About Double Glazed Windows
Near Me double glazed windows near Me
Why We Why We Slot Gaming Experience (And You Should Also!) slot symbols
(zzb.bz)
Guide To Upvc Repairs Near Me: The Intermediate Guide To Upvc Repairs Near Me upvc repairs near Me
What’s The Job Market For SEO Company In London UK Professionals?
seo company in london Uk
5 Killer Quora Answers On Cheap Online Electronics Shopping Uk cheap online electronics shopping uk, Geraldo,
Double Glazing Companies Near Me Tools To Streamline Your Daily Life Double Glazing Companies
Near Me Trick That Every Person Should Know double glazing companies near me
The 10 Scariest Things About Linkedin Content Marketing Linkedin Content Marketing
Expat Jobs
11 Ways To Completely Revamp Your Sofa And Couch Sofas vs Couch
The 10 Most Terrifying Things About Uk Online Shopping Sites For Electronics uk online shopping Sites for electronics
8 Tips For Boosting Your Buying Online From Uk To Ireland
Game online shop (Caitlyn)
Best Seo Backlink Software: The Ugly Facts About Best
Seo Backlink Software best backlinking software (Gabriela)
See What ADHD Adults Test Tricks The Celebs Are
Utilizing Adhd adults Test
Thanks for sharing your thoughts on fitspresso reviews reddit.
Regards
3 Reasons You’re L Shaped Bunk Beds Is Broken (And How
To Fix It) l shaped toddler beds (Vance)
The very best location for installation remains in an open area that
obtains great airflow.