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
viagra canada viagras.online Viagra generic over the counter
It’s difficult to find well-informed people on this topic, however, you seem
like you know what you’re talking about! Thanks
Look at my homepage: lounge chair for sale
A Step-By Step Guide To Selecting Your Misted Double Glazing Repairs Door Repair
Five Mesothelioma Settlement Projects For Any Budget mesothelioma Lawsuit
Replace Mini Key Fob Techniques To Simplify Your Everyday Lifethe Only Replace Mini Key Fob Trick That
Should Be Used By Everyone Learn Replace Mini Key
The 10 Most Scariest Things About Double Glazing Repairs Near Me double glazing repairs (Delia)
You’ll Be Unable To Guess Window Replacement Near Me’s Tricks window replacement near me [Tony]
Cenforce 100mg tablets for sale: cenforce for sale – cenforce.pro
This article is in fact a pleasant one it
helps new internet users, who are wishing for blogging.
Five Killer Quora Answers To Free Casino Slots Casino Slots
Private ADHD Assessment Belfast 101 Your
Ultimate Guide For Beginners book private adhd Assessment
Generic Levitra 20mg: Buy generic Levitra online – Vardenafil price
Mesothelioma Lawyer Commercial Explained In Less Than 140 Characters mesothelioma lawyer Near me
https://levitrav.store/# п»їLevitra price
http://levitrav.store/# Buy Levitra 20mg online
Take A Look At One Of The Upvc Windows Repair Industry’s Steve Jobs Of The
Upvc Windows Repair Industry repair upvc window; https://Olderworkers.com.au/,
Why You’re Failing At Railroad Injuries Legal Vimeo
Viagra online price: Cheap Viagra 100mg – generic sildenafil
5 Killer Quora Answers To Repair Misted Double Glazing Near Me double glazing near me – Omer,
You’ll Be Unable To Guess Car Replacement Keys’s Tricks Car replacement keys
Why No One Cares About Windows Leeds leeds double glazing repairs; Gabrielle,
20 Things You Must Be Educated About Key Programming Car Key Cut And Program
– http://Velo-Xachmas.Com/Index.Php?Subaction=Userinfo&User=Lungeel4 –
Guide To Double Glazing Repairs Near Me: The Intermediate
Guide In Double Glazing Repairs Near Me double glazing repairs Near me
The Ultimate Glossary Of Terms About Personal Injury
Compensation springfield personal injury Lawsuit
Can 18 Wheeler Wreck Lawyer Ever Rule The World?
Vimeo.com
porn site
We bieden tijdelijke telefoonnummers in tientallen landen over de hele wereld, met duizenden nummers om uit te kiezen telegram zonderВ telefoonnummer
How Narwal Robot Vacuum Became The Hottest Trend Of 2023
Self Vacuum Robot
https://cialist.pro/# cialis for sale
Kamagra 100mg: kamagra oral jelly – Kamagra 100mg price