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
mostbet зеркало
Five Killer Quora Answers To Audi A3 Key Replacement key
http://lisinopril.network/# lisinopril price comparison
hysterectomy after breast cancer tamoxifen: tamoxifen rash – tamoxifen lawsuit
Важно! если хотите выращивать плодовые деревья, verin-sad.ru то останавливайтесь низкорослым породам.
Feel free to surf to my web blog: https://oferme.ru/
20 Things That Only The Most Devoted Treatments Of Adhd Fans Should Know adult adhd treatment uk
Who Is The World’s Top Expert On Cheapest Butt Plugs? App Controlled
Buttplug (https://Menwiki.Men/)
10 Of The Top Mobile Apps To Mesothelioma mesothelioma attorney
5 Injury Lawyer Lessons From Professionals Injury lawsuits
zestril 30 mg: prinivil generic – lisinopril 5 mg tablet
What Is The Reason Mobile Diagnostics Is The Right Choice For You?
diagnostics car
It’s appropriate time to make a few plans for the future and
it’s time to be happy. I’ve read this put up and if I could I
wish to suggest you some fascinating things or advice.
Perhaps you could write subsequent articles
referring to this article. I want to read more issues about it!
How Online Home Based Work Rose To The #1 Trend On Social Media work from home no experience
https://finasteride.store/# cost propecia without a prescription
buy cytotec pills: cytotec pills online – Abortion pills online
See What Mobility Scooter Automatic Folding Tricks The Celebs Are Utilizing mobility scooter automatic folding (Gregorio)
This Is The Ultimate Guide To How Do You Unlock A Car Without A Key locksmith
opening car door; Athena,
http://nolvadex.life/# tamoxifen benefits
5 Killer Quora Questions On Childrens Bunk Bed modern bunk Beds
A Trip Back In Time A Trip Back In Time:
What People Talked About Repairs To Upvc Windows 20
Years Ago Window Repairs
Could Window Repair Near Be The Key For 2023’s Challenges?
window repair Near Me
10 Facts About Window Repair That Will Instantly Make You Feel Good Mood glass
15 Things You Didn’t Know About 18 Wheeler Accident Law Firm 18 wheeler Accidents
Every weekend i used to pay a quick visit this web page, for the reason that i wish for
enjoyment, for the reason that this this web site conations really pleasant fuynny material too.
Also visit my website – Aizen Power Reviews
https://cytotec.club/# buy cytotec over the counter
15 Twitter Accounts That Are The Best To Find Out
More About Door Fitters Leeds window doctor leeds
5 Double Glazed Window Leeds Projects For Any Budget Leeds Glazing
http://nolvadex.life/# tamoxifen for gynecomastia reviews
Double Glazing Companies Near Me Tips To Relax Your Daily
Life Double Glazing Companies Near Me Trick
Every Individual Should Learn Double Glazing Companies Near Me
мостбет казино зеркало