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
15 Gifts For The American-Style Fridge Freezers Lover In Your Life American Style Fridges
nolvadex only pct nolvadex d does tamoxifen make you tired
9 Things Your Parents Teach You About Best Online Shopping Sites London Online Shopping Sites
The Unknown Benefits Of Fridge Freezer household
Why You’ll Definitely Want To Learn More About Mesothelioma Legal Question Asbestos Legal
https://lisinopril.network/# buy lisinopril 20 mg online canada
After going over a few of the articles on your site, I
seriously appreciate your technique of blogging.
I added it to my bookmark website list and will
be checking back soon. Please check out my website as well and tell me your opinion.
5 Reasons To Consider Being An Online Treadmill For Sale Business And 5 Reasons Not To Treadmills For Sale Near Me (Dermandar.Com)
ciprofloxacin 500 mg tablet price buy generic ciprofloxacin buy cipro online without prescription
20 Fun Infographics About Repair Misted Double Glazing Near
Me Double Glazing Near Me
https://lisinopril.network/# lisinopril 40 mg tablet price
Five Ghost Immobiliser Price Projects To Use For Any Budget Ghost immobiliser Price
http://cytotec.club/# Cytotec 200mcg price
Five Killer Quora Answers On Replacement Double Glazing Units Near Me
replacement double glazing units near me
buy ciprofloxacin over the counter where can i buy cipro online ciprofloxacin over the counter
raloxifene vs tamoxifen: nolvadex price – nolvadex only pct
http://ciprofloxacin.tech/# ciprofloxacin 500 mg tablet price
The 10 Most Terrifying Things About Audi Key Fob Replacement Audi Key Fob Replacement
(https://Grindanddesign.Com/)
Five Killer Quora Answers To Robot Vacuum Empty Itself Robot Vacuum Empty Itself
8 Tips For Boosting Your Bunk Bed Game best bunk beds for
adults uk (Nagievonline.Com)
The 12 Best Leeds Door And Window Accounts To
Follow On Twitter upvc door repair roundhay (Jisuzm.com)
lisinopril in mexico: 30mg lisinopril – lisinopril 30 mg
20 Insightful Quotes About Affordable SEO Company London affordable search engine marketing uk
You’ll Be Unable To Guess Online Shopping Uk Sites’s Tricks Online Shopping Uk Sites
The 10 Most Dismal Audi A3 Replacement Key Errors Of
All Time Could Have Been Prevented Audi Key replacement
https://finasteride.store/# buy propecia for sale
It Is The History Of Mesothelioma Law Firm In 10 Milestones asbestos case
Воєнторг
12. Маски и балаклавы для скрытности и защиты
воєнторг воєнторг .
http://nolvadex.life/# generic tamoxifen
10 Misconceptions That Your Boss May Have About Private Psychiatrist Diagnosis Private psychiatrist In manchester