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
You’ve made your point very effectively!.
20 Fun Facts About Bentley Key Price lost
7 Easy Tips For Totally Rocking Your Situs Toto Togel bandar Toto
Definitely believe that which you stated. Your favorite justification seemed to be on the net the easiest thing
to be aware of. I say to you, I definitely get annoyed while people consider worries that they just
don’t know about. You managed to hit the nail upon the top and defined out the whole thing without having side effect ,
people could take a signal. Will likely be back to get more.
Thanks
Just want to say your article is as surprising. The clearness for your put up is
just great and that i could suppose you’re knowledgeable in this subject.
Well with your permission let me to seize your feed to stay up
to date with forthcoming post. Thank you one million and please continue the rewarding work.
Amazing blog! Do you have any hints for aspiring writers?
I’m hoping to start my own website soon but I’m a little lost
on everything. Would you advise starting with a free platform like WordPress or go for a paid option? There
are so many choices out there that I’m totally confused ..
Any recommendations? Appreciate it!
Also visit my webpage … thumb latch trigger
https://denemebonusuverensiteler.win/# deneme bonusu veren siteler
I know this if off topic but I’m looking into starting my own weblog and was wondering what all is needed to get set up?
I’m assuming having a blog like yours would cost a pretty penny?
I’m not very web smart so I’m not 100% sure. Any tips or advice would be greatly appreciated.
Thanks
The 15 Things Your Boss Would Like You To Know
You Knew About Citroen Berlingo Van Key Replacement key Cutting service
You definitely made the point!
Look into my blog :: https://images.Google.Co.il/url?q=https://notes.io/wmq3B
в одних вариантах игры помимо этого – используется расклад Фор
флаш («сам-четвёрт») – четыре карты одной масти и одна карта другой
масти.
Visit my web page; https://aiheconglinkb.com/
наркология вывод из запоя в стационаре https://vyvod-iz-zapoya-sochi12.ru .
вывод из запоя в стационаре вывод из запоя в стационаре .
http://slotsiteleri.bid/# slot oyun siteleri
You’ll Be Unable To Guess Mini Cooper Car Key Replacement’s Benefits Mini Cooper Car Key Replacement
http://sweetbonanza.network/# sweet bonanza demo
Five Killer Quora Answers On Repair Bifold Door Top Roller Repair Bifold Door Top Roller
The Ultimate Guide To Electric Fireplace Freestanding Free Standing Log Effect Electric Fires
Every weekend i used to go to see this site, for the reason that i want enjoyment, since this this site conations in fact
fastidious funny information too.
my web blog :: zwoltek01
What’s The Job Market For Daftar Situs Togel Professionals?
daftar situs togel
What’s The Current Job Market For Leather Sectional Sleeper Sofa Professionals?
leather sectional sleeper sofa
A Delightful Rant About What Is Anxiety And Symptoms Male Depression And Anxiety Symptoms
Guide To Coffee Beans Uk: The Intermediate Guide In Coffee Beans Uk coffee beans uk
Have a look at my blog; Daycare Near Me
This Week’s Top Stories Concerning Inattentive ADHD Medication Medication For Odd And Adhd
9 Lessons Your Parents Teach You About Freestanding Electric Stove Fire freestanding Electric stove Fire
You’ll Be Unable To Guess Bentley Keys’s Secrets spare
Aw, this was a very good post. Taking the time and actual effort to make a great article… but what can I say… I hesitate a whole lot and don’t
manage to get nearly anything done.
20 Tips To Help You Be Better At Skoda Car Key
skoda replacement key cost
A Look At The Ugly Facts About Best Double
Bunk Beds bunk bed usa