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
What’s The Job Market For Treadmill Desk Uk Professionals?
treadmill desk Uk (mspeech.Kr)
buy medicines online in india buy medicines from India indian pharmacies safe
http://indiaph24.store/# buy prescription drugs from india
http://indiaph24.store/# indian pharmacy
You definitely made your point!
Five Things You’ve Never Learned About Birth Injury Settlement birth injury lawyers
reputable indian pharmacies https://indiaph24.store/# india pharmacy mail order
pharmacy website india
7 Things About Double Glazed Window Repairs Near Me You’ll Kick
Yourself For Not Knowing double glazed window repair
7 Tips To Make The Most Out Of Your CSGO Case New bravo Case
valtrex medication for sale
How To Determine If You’re In The Right Place For Double Glazing Windows Leeds leeds double glazing repairs
mexico drug stores pharmacies: mexico pharmacy – mexican mail order pharmacies
buy prescription drugs from india https://indiaph24.store/# indian pharmacy online
online shopping pharmacy india
http://mexicoph24.life/# medication from mexico pharmacy
17 Reasons You Shouldn’t Ignore Work Jobs From Home home jobs
п»їbest mexican online pharmacies buying from online mexican pharmacy mexican rx online
15 Surprising Stats About Double Glazing Window Repairs Near Me
double glazing window repair
The 12 Most Popular Double Glazing Repairs Leeds Accounts To Follow On Twitter Window doctor leeds
online shopping pharmacy india https://indiaph24.store/# indian pharmacy online
indian pharmacy paypal
Aw, this was a very good post. Spending some time and actual effort to create a
great article… but what can I say… I hesitate a lot
and don’t manage to get nearly anything done.
9 . What Your Parents Taught You About Upvc Windows Near Me upvc windows near me
Who Is Responsible For The Tommy Hilfiger Xbody Bag Budget?
12 Ways To Spend Your Money cheaper
Designer Handbags To Sell Tips From The Top In The Industry Designer Handbags Sale Outlet Uk
Five Killer Quora Answers On Under Desk Treadmill Uk under desk treadmill uk
20 Quotes That Will Help You Understand Double Glazed Units Manufacturers Near Me Handles
How To Know If You’re Ready For Mens Masturbation Toys Men Masturbator
Hi! Ɗo you know if they make any pluցіns to
protect against hackerѕ? I’m kinda paranoid about losing everything I’ve worked hɑrd on.
Any recommendations?
my site; lihat disini
What’s up everybody, here every one is sharing these experience,
thus it’s pleasant to read this weblog, and I used to visit this weblog daily.
Воєнторг
18. Обмундирование и снаряжение для полевых условий
металошукач металошукач .
I’m impressed, I have to admit. Seldom do I come across a blog that’s equally educative and entertaining, and
without a doubt, you’ve hit the nail on the head.
The issue is something which not enough people are speaking intelligently about.
I’m very happy that I stumbled across this in my search for something relating to this.