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 Job Market For Private ADHD Titration UK Professionals?
Private Adhd Titration
Neymar da Silva Santos Junior https://neymar.prostoprosport-br.com e um futebolista brasileiro que atua como atacante, ponta e atacante. meio-campista do clube saudita Al-Hilal e da selecao brasileira. Considerado um dos melhores jogadores do mundo. O maior artilheiro da historia da Selecao Brasileira.
albuterol ipratropium action
can you buy cheap clarinex for sale
Everything is very open with a really clear explanation of the challenges.
It was really informative. Your site is very helpful. Many thanks
for sharing! https://newtongeniuscenter.co.kr/bbs/board.php?bo_table=free&wr_id=107793
colchicine side effects blood pressure
asbestos Compensation (gurkenmuseum.de) is a naturally occurring mineral that was once used extensively in the construction and manufacturing industries.
The exposure to asbestos was frequently associated with serious and even life-threatening diseases.
Especially those that use corsets and basques for the first time will
certainly fear about this matter.
Hi! This is kind of off topic but I need some help from an established blog. Is it very difficult to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about making my own but I’m not sure where to begin. Do you have any tips or suggestions? Cheers
9 . What Your Parents Taught You About Amazon Online Shopping Clothes Uk Amazon Online Shopping Clothes Uk
can i purchase cheap fincar no prescription
get cheap macrobid online
get cheap differin without rx
There’s certainly a lot to know about this topic. I like all of the points you’ve made.
can you get advair diskus pill
15 Things You Didn’t Know About Cerebral Palsy Lawyers cerebral Palsy Attorneys
Повышайте свои шансы на победу в 1win казино
1win зеркало 1win зеркало .
a knowledge base isn’t just a static repository; it’s a dynamic tool that empowers your team and customers alike.
The Largest Issue That Comes With Windows Repairs Near Me, And How You Can Repair It 257634
Upvc Windows Near Me Tools To Ease Your Daily Lifethe One Upvc Windows Near Me Trick That Should Be Used By Everyone Know Upvc windows near me
where buy generic trazodone prices
Guide To Upvc Repairs Near Me: The Intermediate Guide For Upvc Repairs Near Me upvc repairs Near me
jasminer
avodart excretion
With havin so much content do you ever run into any problems of plagorism or copyright infringement?
My site has a lot of exclusive content I’ve either written myself or outsourced but it appears a lot
of it is popping it up all over the internet without my agreement.
Do you know any solutions to help prevent content from being stolen? I’d definitely appreciate
it.
Camera crew Venice
See What Amazon Uk Online Shopping Clothes Tricks The Celebs Are Utilizing Amazon uk Online shopping clothes
We support the top quality in our products.
my blog :: is lion&39;s mane legit (Finlay#)
The Most Convincing Evidence That You Need Anxiety Disorder Treatments At Home extreme anxiety treatment
Hey! Someone in my Facebook group shared this website with us so I
came to give it a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting
this to my followers! Outstanding blog and superb design.