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
buy cytotec over the counter buy cytotec in usa cytotec online
propecia prices: order propecia without rx – order cheap propecia no prescription
20 Important Questions To ASK ABOUT Double Glazing Repair Cost Before You Decide To Purchase It window repairs [nagievonline.com]
14 Cartoons On Delonghi Nespresso Machine That’ll Brighten Your Day pink nespresso machine
What Is CSGO Opening Sites And Why Is Everyone Speakin’ About It?
case Operation Bravo
Whoa! This blog looks just like my old one! It’s on a entirely
different topic but it has pretty much the same layout and design. Outstanding choice of colors!
order propecia no prescription get generic propecia without a prescription cost generic propecia no prescription
lisinopril pills 2.5 mg drug prices lisinopril lisinopril 3973
nolvadex vs clomid: tamoxifen premenopausal – nolvadex pills
A Step-by-Step Guide To Selecting The Right Adhd Treatments Treatment for adhd in adults Uk
https://finasteride.store/# cost of cheap propecia prices
Why You Should Be Working With This Double Bunk Bed sturdy bunk beds
Five Killer Quora Answers To Replacement Double Glazing Units Near Me replacement double glazing units near me (Eve)
This Week’s Top Stories Concerning Birth Defect Claim birth defect law firms (Wally)
cytotec pills online: buy cytotec online – order cytotec online
How Ghost Immobiliser Problems Has Become The Top Trend On Social Media ghost Immobiliser installation
Hello i am kavin, its my first time to commenting anywhere, when i read this piece of writing i thought i could
also make comment due to this brilliant article.
Thanks in support of sharing such a fastidious thinking, piece of writing is nice, thats
why i have read it entirely
my site; Explore forex trading platforms
The 10 Most Terrifying Things About Emergency Car Key
Replacement emergency car key replacement (Dustin)
get propecia without dr prescription: cost generic propecia online – how cÉ‘n i get cheap propecia pills
https://nolvadex.life/# tamoxifen cancer
If you have noticed your child’s struggles with ADHD it could be time to take an immediate Adhd in adults symptoms test symptoms test.
These tests are crucial because they let you and your physician determine which treatment option is best for your child.
You’ll Never Be Able To Figure Out This Mobile
Car Diagnostics’s Benefits car diagnostics near Me
Nine Things That Your Parent Taught You About
Mesothelioma Litigation Mesothelioma litigation
Five Things You’re Not Sure About About Diagnostics
Near Me mobile diagnostics near me
Double Glazing Companies Near Me Tips To Relax Your Daily Life Double Glazing
Companies Near Me Trick That Everybody Should Know double glazing companies Near me
http://lisinopril.network/# can i buy generic lisinopril online
lisinopril 10mg daily: buy lisinopril online no prescription – 30mg lisinopril
Search Engine Optimization Techniques To Simplify Your
Daily Lifethe One Search Engine Optimization Trick That Everyone Should Learn search engine optimization Marketing
Appreciate it. Numerous knowledge.