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
Great Tips In Winning At Roulette 무료슬롯 프라그마틱 – http://jensen-consultants.com/__media__/js/netsoltrademark.php?d=minecraftcommand.science/profile/petmuscle86 –
http://remontwasher.ru/
A Guide To Veterans Disability Lawsuit In 2023 veterans disability law
firm (Senaida)
A Few Practical Things On Obtaining A Renewed
Car Finance 신생아 특례 대출
order durex gel online cheap – buy zovirax cheap where to buy xalatan without a prescription
slot
How Do Those “Fast” Backlink Services Work? 구글SEO (neucapone.com)
When I initially left a comment I appear to have clicked the
-Notify me when new comments are added- checkbox and now every time a comment is added I recieve
four emails with the exact same comment. There has to be a means you are able to
remove me from that service? Cheers!
The 10 Most Scariest Things About Upvc Repairs Near
Me Upvc repairs near Me
Webinar On Seo By Susanne Myers: Seo Strategies That Work 구글SEO
What’s The Reason Everyone Is Talking About Medical Malpractice Lawsuit Right Now medical malpractice law firm
It’s perfect time to make a few plans for the longer term and it’s time to be
happy. I have learn this submit and if I may just I desire to counsel you few attention-grabbing issues or advice.
Maybe you can write subsequent articles relating to this article.
I desire to learn more issues approximately it!
Medicine information sheet. What side effects?
where can i get arimidex online
Everything news about medicament. Get information now.
The 10 Scariest Things About Double Glazing Company Near Me Double Glazing Company Near Me
The Three Greatest Moments In Locksmiths Near Me For Car History elsycrays
Hello, I enjoy reading through your article. I wanted to write a little comment to support you.
When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment
is added I get several e-mails with the same comment.
Is there any way you can remove people from that service?
Cheers!
untrustable
I love it when people come together and share views.
Great site, stick with it!
The Spare Car Key Cut Awards: The Most Stunning, Funniest, And Strangest Things
We’ve Ever Seen http://www.99811760.xyz
hacklink
Why Do So Many People Want To Know About Veterans Disability Case?
Attorneys
Drugs prescribing information. What side effects?
where to get zanaflex prices
All news about medicament. Read information here.
See What Replacement Double Glazed Glass Only Near Me Tricks The Celebs Are Using replacement double glazed glass Only
It’s remarkable in support of me to have a site, which
is useful for my experience. thanks admin
What Is Veterans Disability Attorney? Heck What Is Veterans Disability Attorney?
veterans disability lawsuits
I love looking through a post that can make men and women think.
Also, thank you for permitting me to comment!
I truly love your blog.. Pleasant colors & theme. Did you make this amazing
site yourself? Please reply back as I’m wanting to create my very
own site and want to learn where you got this
from or what the theme is called. Appreciate it!
I am extremely inspired together with your writing abilities as smartly as with the
format to your weblog. Is that this a paid theme or did you modify it yourself?
Anyway stay up the nice quality writing, it is uncommon to look a
great weblog like this one nowadays..
See What Double Glazing Repairs Near Me Tricks The Celebs Are Using double glazing repairs near me; mournheim.com,