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
Are You Confident About Local SEO Strategy? Check
This Quiz in my area
It’s Time To Expand Your Titration Options Options
cipro ciprofloxacin: buy cipro online canada – antibiotics cipro
cipro ciprofloxacin buy cipro online ciprofloxacin order online
Amazing material Kudos.
http://finasteride.store/# buy cheap propecia pill
nolvadex half life: is nolvadex legal – aromatase inhibitors tamoxifen
Unexpected Business Strategies That Helped Mesothelioma
Settlement Achieve Success asbestos case (Efren)
See What Double Glazing Repairs Near Me Tricks The Celebs
Are Using Double Glazing repairs near me
Cytotec 200mcg price: buy cytotec in usa – order cytotec online
http://lisinopril.network/# lisinopril in mexico
9 Lessons Your Parents Taught You About Affordable SEO
Company London Affordable seo company – pennington-boyette-2.technetbloggers.de,
http://lisinopril.network/# medication zestoretic
How To Become A Prosperous Remote Control Anal Plugs Entrepreneur Even If You’re Not Business-Savvy silicone anal plugs
[https://www.dermandar.com/user/sheepdragon5]
lisinopril 20mg tablets price lisinopril pill 40 mg rx drug lisinopril
20 Reasons Why Untreated Adhd In Adults Cannot Be Forgotten Treat Adult Adhd
What’s The Job Market For Double Glazed Window Repairs
Near Me Professionals Like? window repairs Near me
How To Tell The Good And Bad About Double Glazing Installers Near Me
double glazed Windows handles
Five Tools Everybody Involved In Perfumes Avon Industry Should Be Making Use Of
avon Perfume on sale
You Are Responsible For An CSGO Cases Ranked Budget? 12 Top Notch Ways To
Spend Your Money chroma 3 case (Rusty)
buy cipro online ciprofloxacin mail online ciprofloxacin order online
tamoxifen and grapefruit where can i buy nolvadex tamoxifen dose
13 Things About CSGO Cases Explained You May Never Have Known Chroma 2 Case
buy cytotec pills online cheap cytotec abortion pill buy cytotec online fast delivery
Central Locking Key Repair Tools To Help You Manage Your
Everyday LifeThe Only Central Locking Key Repair Trick That
Everyone Should Know central Locking key repair [ward-lunde.hubstack.net]
The Intermediate Guide The Steps To Upvc Windows
Repair Double glazed window repairs near me
10 Easy Steps To Start Your Own Ford Replacement Key Programming Business ford car key replacement
The 15 Things Your Boss Wishes You’d Known About CS GO Weapon Case esports 2013 case
(http://www.301gm.com)
https://finasteride.store/# propecia pills
http://lisinopril.network/# lisinopril 80