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
How To Outsmart Your Boss Cheapest Washing Machine 10kg Washing machine 10kg
It’s remarkable for me to have a web site, which is valuable for my know-how.
thanks admin
lisinopril cost 5mg lisinopril 40 mg mexico lisinopril 15mg
20 Reasons To Believe Tommy Hilfiger Crossbody Handbag
Will Never Be Forgotten Tommy hilfiger Bag Laptop
lisinopril 18 mg where can i order lisinopril online lisinopril 20 25 mg tab
20 Reasons Why SEO Service Uk Cannot Be Forgotten Seo Services In London [Esbensen-Wang-2.Blogbright.Net]
The Congo African Grey Parrot is the largest
and more commonly known of the two types of the African Grey Parrot.
https://cytotec.club/# Abortion pills online
cytotec online: cytotec abortion pill – buy cytotec in usa
10 Quick Tips For 12kg Washing Machine Uk buy 12kg washing
machine (Anitra)
https://ciprofloxacin.tech/# where can i buy cipro online
I really love your site.. Very nice colors & theme. Did you create this site yourself?
Please reply back as I’m looking to create my very own site and would like to find
out where you got this from or what the theme is called.
Kudos!
It’s remarkable in favor of me to have a website, which is useful designed for my knowledge. thanks admin
https://englishmax.ru/
15 Reasons Not To Ignore Online Shopping Sites For Clothes Raisin Boat Shoes Men
Reenergized
4434 Pacific Coast Hwy,
ᒪong Beach, СA 90804, United Stateѕ
562-689-9888
brain Tap cost therapy near me
This Is A Double Glazed Window Near Me Success Story You’ll Never Be Able To Double Glazed window
Check Out: How Designer Handbags Black Is Gaining Ground, And What Can We
Do About It Designer Handbags Amazon
15 Gifts For The Washing Machine 10kg Uk Lover In Your Life washing machine 10kg uk
10 Things Everybody Gets Wrong About Slot Apps Top-rated Slots
http://finasteride.store/# propecia without a prescription
cost cheap propecia price: propecia otc – generic propecia without dr prescription
Question: How Much Do You Know About Window Repair Near?
upvc window repairs (Curtis)
cipro online no prescription in the usa buy cipro ciprofloxacin order online
10 Apps To Help Manage Your How Much Are Mobility Scooters To
Buy Best Mobility Scooter For Steep Hills Uk
https://lisinopril.network/# lisinopril 20 mg mexico
What’s The Current Job Market For Replacement Window Glass Near Me Professionals?
replacement window glass near me (0522224528.ussoft.kr)
7 Things About Stoves Defra Approved You’ll Kick Yourself For Not Knowing
Defra List stoves
Great blog here! Additionally your website quite a
bit up fast! What web host are you the usage of?
Can I am getting your affiliate hyperlink to your
host? I want my website loaded up as quickly as yours
lol
The Most Prevalent Issues In Case Key CSGO Fracture case
lisinopril tabs 20mg zestril cost lisinopril 40 mg best price