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
Guide To Online Clothes Shopping Near Me: The Intermediate Guide Towards Online Clothes Shopping Near Me online clothes shopping Near me
The Unspoken Secrets Of Personal Injury Case Personal Injury Attorney
Pvc Window Repairs It’s Not As Expensive As You Think Upvc Window Repair
I’m the proprietor operator of GOPARTYBUS Perth.
My web site: Bookmarks
The Best Motor Vehicle Settlement Methods To Transform Your Life Motor vehicle accident Lawyer
Why You Should Focus On Making Improvements To Private Adhd Assessment Adhd assessments
Responsible For The Repairing Upvc Windows Budget?
10 Amazing Ways To Spend Your Money upvc windows repair near Me
The History Of Double Glazed Units Near Me Replacement double glazed units near me
where can i buy generic betnovate price
Hi, I do think this is a great web site. I stumbledupon it 😉 I
may revisit once again since i have saved as a favorite it.
Money and freedom is the best way to change, may you be rich
and continue to help other people.
What is New U Life SOMADERM Transdermal Gel.
My website; types of disability benefits
can i order generic valtrex pills
Why Motor Vehicle Lawsuit Is Harder Than You Imagine Motor Vehicle Accident Lawsuit
5 Clarifications Regarding Birth Injury Lawyers Birth Injury Attorney
Superb website you have here but I was curious about if you knew of any message boards
that cover the same topics talked about in this article? I’d really like to be a
part of online community where I can get advice from other experienced people that share the same interest.
If you have any recommendations, please let me know.
Many thanks!
What Is Birth Injury Lawyer And Why Is Everyone Speakin’ About
It? birth injury Law firms
5 Killer Quora Answers On Shopping Online Uk Clothes shopping online uk
clothes – teploenergodar.ru,
Allow me show you in my evaluation of NewULife.
my web blog; ai profile picture generator reddit
can i get valtrex pills
оборудование для конференц связи https://oborudovanie-konferenc-zalov11.ru .
Финансовые платежи, бонусы и верификация вынесены в отдельные блоки, вне личного кабинета пользователя.
современный конференц зал http://oborudovanie-konferenc-zalov11.ru/ .
оборудование для конференц зала готовые решения http://oborudovanie-konferenc-zalov11.ru/ .
конференц зал оборудование оснащение конференц зал оборудование оснащение .
how to get cheap stromectol pills
7 Simple Secrets To Totally Rocking Your Upvc Doors Repair replace Lock on upvc door
Super Slots will double each and every transaction uup to
$1,000, givinjg you a total of $six,000.
Also visit my web blog; 먹튀검증
This brand uses organic hemp to make all its products.
Here is my web page :: Bookmarks
17 Reasons Not To Not Ignore 18 Wheeler Truck Accident Attorney 18 wheeler Accident
The Guide To Affordable SEO Expert In 2023 Affordable Seo Packages Usa