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
14 Questions You Shouldn’t Be Refused To Ask Online Shop
Which Supermarket Is cheapest for online shopping
Kia Key Fob Replacement Tips That Will Change Your
Life How To Open Kia Car Key
I am really happy to read this weblog posts which consists of tons of helpful data, thanks for providing such data.
Fantastic beat ! I would like to apprentice while
you amend your website, how can i subscribe for a blog site?
The account aided me a acceptable deal. I had been a little bit acquainted of this your
broadcast provided bright clear idea
The Biggest Problem With Online Shopping Sites For Clothes, And How You Can Fix
It Us Online Shopping Sites For Clothes
How To Outsmart Your Boss On Truck Accident Truck accident attorney
Every weekend i used to pay a quick visit this site,
because i wish for enjoyment, for the reason that this this web page conations truly
good funny data too.
I have read so many content regarding the blogger lovers however this piece of writing
is in fact a nice paragraph, keep it up.
I was suggested this web site by my cousin. I’m not sure
whether this post is written by him as nobody else know such detailed about
my problem. You are incredible! Thanks!
5 Must-Know Online Shopping Uk Electronics Practices
For 2023 No Gemstone Silicone Ring
How Much Can Double Glazed Window Repairs Near Me Experts Earn? company
Why We Why We Online Shopping Sites In United Kingdom (And You Should, Too!) Bx-1 Yosemite Optics
Amazing facts Kudos!
I am truly thankful to the holder of this web site who has shared
this wonderful article at here.
Сотрудники компании Сантехника-Онлайн оценивают работу руководства на 4.1 из 5, на основании 165 оценок. Как сотрудники Сантехника-Онлайн оценивают возможности карьерного роста компании Отзывы сотрудников Сантехника Онлайн
비아그라 대체 약품의 부상
비아그라 대체 약품의 부상: 안전하고 효과적인 옵션 탐색
Medicine prescribing information. What side effects can this medication cause?
why the cough with lisinopril
Everything news about medicines. Get information now.
Why You Should Concentrate On Making Improvements To Best Slot Machines Multi-Line Slots
Concert Attire Stamford
360 Fairfield Ave,
Stamford, CT 06902, United Ꮪtates
+12033298603
Sneakers
I do not even know how I ended up here, but I thought
this post was great. I don’t know who you are but certainly you
are going to a famous blogger if you are not already 😉
Cheers!
Feel free to surf to my web page fun88
What The Heck What Is Slot Trends? Top software providers for slots
Ten Stereotypes About Low Limit Slots That Aren’t Always
True evoplay slots strategy (http://the-good.kr)
You’ll Never Guess This Online Shopping Uk Amazon’s Tricks Online Shopping Uk Amazon
can i get generic aristocort
The 9 Things Your Parents Taught You About Examples Of Online Shopping examples Of online shopping
I was recommended this website by my cousin. I am not
sure whether this post is written by him as no one else know such detailed about my
problem. You’re incredible! Thanks!
Review my web page; Icam Server
The 10 Most Terrifying Things About Shopping Online industrial arbor press (https://vimeo.com)
Caranya dengan mengklik foto tersebut dan memilih opsi download yang sudah disediakan oleh Twitter.
4 Dirty Little Details About Best Slot Machines Industry Best Slot Machines Industry
multi-line slots (https://www.google.st/url?q=https://rainbet.com/pt/casino/slots/smartsoft-evolution)
The Best Advice You Could Ever Receive On Classic Casino Slots entertaining slots (http://wx.abcvote.cn/)