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
See What Mesothelioma Litigation Tricks The Celebs Are Using Mesothelioma Litigation
purchase cipro cipro ciprofloxacin buy cipro online canada
tamoxifen hot flashes nolvadex only pct how does tamoxifen work
http://finasteride.store/# propecia brand name
A Step-By-Step Instruction For Best Slot Payouts Popular Slots
generic propecia: cost generic propecia pill – get propecia without rx
https://nolvadex.life/# hysterectomy after breast cancer tamoxifen
http://cytotec.club/# cytotec online
When someone writes an piece of writing he/she
keeps the plan of a user in his/her mind that how
a user can know it. Therefore that’s why this article is perfect.
Thanks!
Is Citroen Replacement Key Cost As Important As Everyone Says?
Citroen Xsara Key
http://lisinopril.network/# lisinopril tabs
10 Things Everyone Hates About Bean To Cup Coffee Machines coffee machine beans to cup
Upvc Windows Near Me Tools To Streamline Your Daily Lifethe One Upvc
Windows Near Me Trick That Everyone Should Learn upvc windows near me
What Is Double Mattress Memory Foam? And How To Utilize It latex-free
Hi, I do believe this is an excellent blog. I stumbledupon it 😉 I
may return once again since i have book marked it.
Money and freedom is the greatest way to change, may you be rich and continue to guide other people.
Five Killer Quora Answers On Double Glazed Near Me Double Glazed Near Me
Have you ever considered writing an e-book or
guest authoring on other blogs? I have a blog
based upon on the same information you discuss and would really like to have you share some stories/information. I
know my viewers would enjoy your work. If you’re even remotely interested,
feel free to send Physiotherapie Near Me an e mail.
cost of propecia online: cost of cheap propecia price – cost propecia without insurance
Why You Should Concentrate On Improving ADHD Tests For Adults Adhd online testing
The History Of Winning Slots play Slots
http://ciprofloxacin.tech/# buy generic ciprofloxacin
Hi i am kavin, its my first time to commenting anywhere, when i read this post i thought i could also make
comment due to this sensible post.
my web-site … classic free movies
Awesome posts, Thanks.
4 Dirty Little Tips About Repair A Window Industry Repair A Window Industry
Window Repairs
Locksmiths Near Me Auto Is The Next Hot Thing In Locksmiths Near Me Auto Auto key smith near me
Good posts With thanks!
Hey I know this is off topic but I was wondering if you knew of any widgets
I could add to my blog that automatically tweet
my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe
you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy reading
your blog and I look forward to your new updates.
See What Double Glazed Windows Near Me Tricks The Celebs Are Using double glazed windows Near me
http://ciprofloxacin.tech/# п»їcipro generic
Guide To Managing Adhd Without Medication: The Intermediate Guide Towards Managing Adhd Without Medication Adhd Without medication