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
10 Meetups About Upvc Window Repairs You Should Attend upvc window Repairs near me (http://web011.Dmonster.kr/bbs/board.php?bo_table=b0501&wr_id=1769266)
Fascinating blog! Is your theme custom made or did you download it from somewhere?
A theme like yours with a few simple adjustements
would really make my blog stand out. Please let me know where
you got your design. Bless you
cipro: cipro 500mg best prices – ciprofloxacin 500mg buy online
casino online con bono de bienvenida sin deposito https://www.sin-deposito-peru.org/ .
Cytotec 200mcg price: Misoprostol 200 mg buy online – Abortion pills online
can you buy metformin over the counter in uk
casino online con bono de bienvenida sin deposito http://www.sin-deposito-peru.org/ .
Check Out: How Trucking Lawyers Is Taking Over And What You Can Do About It denver truck accident attorney
20 Trailblazers Lead The Way In Slot Casino casino slot tournaments (Roxanne)
https://cytotec.club/# buy cytotec pills
casino con bono de bienvenida sin deposito https://www.sin-deposito-peru.org/ .
15 Great Documentaries About Window Repairs upvc
window repairs; lindberg-Emborg-2.Mdwrite.net,
My partner and I stumbled over here different web page and thought I might check
things out. I like what I see so i am just following you.
Look forward to going over your web page yet again.
What’s The Reason? Tommy Hilfiger Purse Bag Is Everywhere
This Year tommy Hilfiger Black bag
10 Healthy Double Glazed Window Repair Habits misted
casino con bono de bienvenida sin deposito https://www.sin-deposito-peru.org/ .
What Are The Biggest “Myths” Concerning Double Glazing Spares Near Me
Could Actually Be True replace Double glazed Units
obviously like your web site however you have to check the spelling on quite
a few of your posts. Many of them are rife with spelling issues and I in finding it very bothersome to inform the reality on the other hand
I will definitely come again again.
Hello mates, pleasant paragraph and nice arguments commented here, I am actually enjoying by these.
20 Things You Need To Be Educated About Auto Accident Legal Auto accident law firms
https://nolvadex.life/# tamoxifen cyp2d6
Five Killer Quora Answers On Popular Casino Slots popular Casino slots
Amazing issues here. I’m very glad to look your article.
Thanks a lot and I’m having a look forward to touch you.
Will you please drop me a mail?
https://finasteride.store/# order generic propecia no prescription
Are You Getting The Most You Imari Avon Perfume?
imari Avon perfume
http://lisinopril.network/# online pharmacy lisinopril
http://cytotec.club/# buy cytotec pills
The 12 Best Mesothelioma Lawyer Accounts To Follow On Twitter asbestos Case
tamoxifen pill tamoxifen for sale tamoxifen and depression
http://nolvadex.life/# tamoxifen and grapefruit