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
An asbestos lawyer can assist victims file a suit against the business that exposed them to Asbestos Lawsuit.
A lawsuit can help victims obtain compensation for medical expenses as well as lost wages
and other damages.
how to buy atarax without a prescription
where to buy cheap inderal without dr prescription
You’ll Be Unable To Guess Online Shopping Sites Top 7’s
Benefits online shopping sites top 7
14 Creative Ways To Spend The Leftover Coffee Machine Budget http://www.4182051.xyz
You’ll Never Guess This Casino Slots’s Tricks kaymell.uk
Looking Into The Future What’s The Online Shopping Industry Look Like In 10 Years?
Best Luxury Online Shopping Sites Uk
It’s The Ugly Truth About Boot Mobility Scooter arlennizo
When someone wгites an paragraph hе/she retains the thought of a user in his/her
brtain thhat how a user can understand it. Tһus that’s whyy
this paгаgraph is amazing. Thаnks!
Feel frreе to surf to mуy blog post :: swap pdf
where buy cheap astelin online
20 Up-Andcomers To Watch The American Fridge Freezer
Sale Industry zackfoxworth
Ten Stereotypes About Why Are CSGO Skins Going Up In Price That Aren’t Always True cs2 cases
Is Your Company Responsible For An Adhd Assessments For Adults Budget?
12 Best Ways To Spend Your Money adhd assessment for adults what to expect
20 Fun Facts About Nissan Qashqai Key Fob nissan xterra key fob
Wooden Loft Bed Isn’t As Difficult As You Think bed loft (Darnell)
cheap doxycycline without insurance
I’ve been exploring for a bit for any high-quality articles or blog posts in this sort of house
. Exploring in Yahoo I ultimately stumbled upon this web site.
Studying this info So i am glad to convey that I’ve an incredibly good uncanny feeling I discovered
exactly what I needed. I so much certainly will make certain to
don?t disregard this site and give it a glance regularly.
Medicine information sheet. What side effects can this medication cause?
can i purchase generic ampicillin pill
Everything trends of pills. Get information here.
Всем привет! Может кто знает, где найти разные статьи о недвижимости? Сейчас читаю https://an72.ru
May I just say what a relief to uncover someone who actually knows what they’re discussing on the net.
You actually realize how to bring a problem to light and make it important.
A lot more people should look at this and understand this side of the story.
I was surprised you aren’t more popular because you
most certainly possess the gift.
You’ll Never Guess This Best Online Shopping Sites Clothes’s Tricks best online shopping sites clothes
Thanks on your marvelous posting! I seriously enjoyed reading it,
you might be a great author.I will make sure
to bookmark your blog and will come back in the future.
I want to encourage that you continue your great writing, have a nice morning!
9 Things Your Parents Taught You About Upvc Window Repairs
Near Me Window Repairs near me
Hey! This is my 1st comment here so I just wanted to give a quick shout out and say I truly enjoy
reading your articles. Can you recommend any other blogs/websites/forums that deal with the same topics?
Many thanks!
You made some nice points there. I did a search on the subject and found most guys will agree with your website.
Also visit my blog post; https://vavadaonlinecasino.evenweb.com/
jasminer
15 Things You Didn’t Know About Accident Lawyer Accident attorney
Could Coffee Pod Machine Be The Key For 2023’s Challenges?
4182051.xyz
The Most Effective Advice You’ll Ever Receive About Semi Truck Lawsuit semi truck Accident lawyers