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
Ten Cerebral Palsy Lawyers That Will Actually Make Your Life Better cerebral palsy lawsuit
Saved as a favorite, I love your website!
Lorem Ipsum has been the industry’s standard dummy text https://salvatierrasostenible.es/esgtqb of type and scrambled it to make a type specimen book.
If you are going for most excellent contents like me, simply
pay a quick visit this web page daily because it gives quality contents,
thanks
Feel free to surf to my website – genius wave reviews
Of course, commercial casinos represent only one type of gambling recognized by the American Gambling
Association (AGA).
my webpage; 토토친구
See What Window Repairs Near Me Tricks The Celebs Are Making Use Of Window Repairs Near Me
Medicines information. What side effects can this medication cause?
where can i get generic kamagra prices
Everything information about medicament. Get here.
Virgil van Dijk https://virgilvandijk.prostoprosport-br.com Futebolista holandes, zagueiro central, capitao do clube ingles Liverpool e capitao do a selecao holandesa.
Are You Getting The Most The Use Of Your Online Shopping Sites For Clothes?
Shopping Online
20 Resources That Will Make You More Successful At ADHD Private Diagnosis Cost
private Adhd assessment cost
We’re a group of volunteers and opening a new scheme in our community.
Your site offered us with valuable information to work
on. You’ve done an impressive job and our entire community
will be grateful to you.
Dangerous Drugs Lawyer Tools To Help You Manage Your
Daily Life Dangerous Drugs Lawyer Trick Every Individual Should Know Dangerous drugs Lawyer
What is New U Life SOMADERM Transdermal Gel.
Feel free to surf to my web page – type 1 diabetes disability benefits uk
The IQ is the very best dry natural herb vegetable based vape juice I have actually attempted.
Hey there just wanted to give you a quick heads up.
The text in your article seem to be running off the screen in Ie.
I’m not sure if this is a formatting issue or something to do with web
browser compatibility but I figured I’d post to let you
know. The design look great though! Hope you get the problem resolved
soon. Many thanks
How To Ship To Ireland From Uk Tools To Streamline Your Everyday Lifethe Only How To Ship To Ireland From
Uk Trick Every Person Should Be Able To How To Ship To Ireland From Uk (Latenitetip.Com)
Wow! In the end I got a blog from where I be able to genuinely take valuable information regarding my
study and knowledge.
Drugs information sheet. What side effects?
where to buy generic cipro prices
Some what you want to know about drugs. Read now.
Whats up are using WordPress for your blog platform?
I’m new to the blog world but I’m trying to get started and set up my own. Do you need any html coding knowledge to make your own blog?
Any help would be greatly appreciated!
Let me reveal you in my testimonial of NewULife.
Have a look at my webpage; motion graphics online editing
Pills information. Drug Class.
where buy anastrozole pill
Some news about medicament. Get information here.
can i purchase cheap cozaar without a prescription
Thanks a lot! I like this!
my site; http://woodhyun.com/bbs/board.php?bo_table=free&wr_id=466669
You have made some decent points there. I checked on the web for more info about
the issue and found most individuals will go along with your views on this website.
can i get generic avodart without prescription
NewULife Ranks. Invite to my NewUlife Review.
Look at my homepage; best Ai headshot generator reddit
Fastidious respond in return of this issue with solid arguments and
telling the whole thing on the topic of that.
I loved as much as you’ll receive carried out right here.
The sketch is attractive, your authored material stylish.
nonetheless, you command get bought an shakiness over that you wish be delivering the following.
unwell unquestionably come more formerly again since exactly
the same nearly a lot often inside case you shield this increase.
Meds information for patients. Short-Term Effects.
how can i get cheap nolvadex for sale
Some trends of drug. Read information here.
where buy cheap lisinopril tablets