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
Do You Think You’re Suited For CSGO Most Profitable Cases?
Try This Quiz Prisma Case
tamoxifen citrate tamoxifen and grapefruit tamoxifen rash pictures
10 Reasons You’ll Need To Learn About Car Key Cover Fiat 500 Fiat panda Key programming
http://lisinopril.network/# prinivil
nolvadex price: femara vs tamoxifen – tamoxifen hot flashes
http://finasteride.store/# generic propecia without prescription
A Positive Rant Concerning Upvc Window Repairs Upvc Window Repairs Near Me
order lisinopril from mexico zestril lisinopril 15 mg tablets
I am extremely inspired along with your writing skills as neatly as with the format for your
blog. Is that this a paid topic or did you modify it yourself?
Either way stay up the excellent high quality writing, it’s rare to peer a nice weblog like
this one these days..
Here is my web page :: Gold filled jewelry gifts
Misted Double Glazing Repairs Tools To Ease Your Daily Life Misted Double
Glazing Repairs Trick That Should Be Used By Everyone Learn double glazing repairs (Colette)
lisinopril 25 mg tablet lisinopril 500 mg drug prices lisinopril
Everything You Need To Know About Volvo Digger Key
Dos And Don’ts Car key Volvo
cipro 500mg best prices cipro online no prescription in the usa buy cipro online without prescription
What’s The Job Market For Car Key Repair Shop Near Me Professionals Like?
Key Repair Shop Near Me
The Best Online Shopping Uk Electronics Tricks To Rewrite Your Life Comfortable Breastfeeding Pads
How Window Repair Near Has Become The Most Sought-After Trend Of 2023
window repair Near Me
Injury Legal Explained In Less Than 140 Characters injury Lawsuit
How To Create Successful Car Lock Repair Near Me Instructions
For Homeschoolers From Home car central locking repair near Me
propecia rx: cost of cheap propecia online – get generic propecia pills
This Is How Bunk Beds Double Bed Will Look Like In 10 Years double bed Bunk – https://www.dermandar.com/ –
cheapest price for lisinopril: lisinopril 25 mg cost – lisinopril cost uk
tamoxifen breast cancer prevention: tamoxifen breast cancer prevention – tamoxifen citrate pct
https://lisinopril.network/# buy lisinopril 20 mg online united states
https://lisinopril.network/# lisinopril 10 mg on line prescription
Do Not Believe In These “Trends” Concerning Robot Vacuum And Mops robot vacuum
and mop self empty – Leonardo –
http://finasteride.store/# buying propecia tablets
The 10 Most Scariest Things About Pvc Window Repairs window repair
Need Inspiration? Check Out Lock Smith For Cars Car Lock Smith
https://finasteride.store/# cheap propecia without insurance
What’s up, I want to subscribe for this blog to take
newest updates, thus where can i do it please help out.