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
What’s The Current Job Market For Window Doctor Near Me Professionals?
Window Doctor Near Me (Hildebrandt-Kaas.Blogbright.Net)
Explore the dynamic world of sports https://noticias-esportivas-br.org through the lens of a sports reporter. Your source for breaking news, exclusive interviews, in-depth analysis and live coverage of all sports.
I always spent my half an hour to read this webpage’s articles or reviews every day along with a cup of
coffee.
Here’s A Few Facts Concerning Coffee Machine Pods 4182051.xyz
buying cheap valtrex tablets
A site dedicated to Michael Jordan https://michael-jordan.uz, a basketball legend and symbol of world sports culture. Here you will find highlights, career, family and news about one of the greatest athletes of all time.
ksm 66 ashwagandha near me
15 Top Twitter Accounts To Find Out More About Coffee Machine For Home 4182051.xyz
Why Is Window Replacement Near Me So Famous?
Glazing
How To Make An Amazing Instagram Video About Demo Pragmatic Zeus
slot game zeus (Eleanor)
This piece of writing will help the internet users for creating new blog or even a blog from
start to end.
The 10 Most Scariest Things About Upvc Door Repairs Near Me Upvc Door Repairs Near Me
You’ll Be Unable To Guess Personal Injury Lawyers’s Tricks personal injury Lawyers
Find the latest information on Conor McGregor https://conor-mcgregor.uz news, fights, and interviews. Check out detailed articles and news about McGregor’s UFC career, wins, training, and personal life.
The Most Hilarious Complaints We’ve Heard About Bunk Bed Shop eddafay
Five Most Effective ADHD Medication For Adults Lessons From
The Professionals Adhd Medication Uk Buy Online – http://Www.Alonegocio.Net.Br,
10 Meetups On How Much Is A Private ADHD Assessment You
Should Attend private Adhd Assessment medway
Hey this is kind of 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 experience so I wanted
to get advice from someone with experience. Any help would
be enormously appreciated!
Discover how Riyad Mahrez https://al-ahli.riyad-mahrez.com transformed Al-Ahli, becoming a key player and catalyst in reaching new heights in world football.
Hurrah, that’s what I was seeking for, what a stuff! existing here at this
weblog, thanks admin of this website.
If yoou selext real income slots, you can win large jackpots online.
Feel free too viit my web blog: 비트코인 소프트웨어
Sweet blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how
to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there!
Thanks
my web-site – http://superpremium2.premium4best.eu/
Всем привет! Подскажите, где почитать полезные блоги о недвижимости? Сейчас читаю https://yah-bomag.ru
Learn About Treatment Of ADD In Adults While You Work From The Comfort Of Your Home doctors who treat adhd in adults near Me
(Aragaon.net)
Hades What Is God Mode Tips To Relax Your Daily Life Hades What Is God Mode Trick
Every Person Should Know Hades What Is God Mode
What’s The Job Market For Window Doctor Near Me Professionals Like?
window Doctor near me – http://www.stes.tyc.edu.Tw –
Why We Our Love For Designer Handbags Uk Sale (And You Should Too!)
http://www.836614.xyz
10 Best Mobile Apps For Veterans Disability Law Veterans disability lawyers
Responsible For The Treatments For Adult ADHD Budget?
12 Ways To Spend Your Money treatment for adhd (Zimbra.tensoft.kr)
7 Simple Tricks To Totally Doing The Double Glazing Near
Me double Glazed Door handle