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
Hi there, I log on to your blog on a regular basis. Your writing style is witty, keep up the
good work!
You’ll Never Guess This Double Glaze Repair Near Me’s Tricks
double glaze repair near me
get cheap super p-force tablets
Guide To Double Glazing Window Repairs: The Intermediate Guide To
Double Glazing Window Repairs Window Repairs, Maps.Google.Tg,
Meds information leaflet. What side effects?
cheap anastrozole without a prescription
Actual information about medicine. Get now.
The Most Common Mistakes People Make With Semi
Truck Legal semi truck accident lawsuit [escortexxx.ca]
Lorem Ipsum has been the industry’s standard dummy text https://onkopaednki.de/tvqrns of type and scrambled it to make a type specimen book.
15 Of The Best Twitter Accounts To Discover U
Shaped Sectional Leather small u shape sofa (whippet-insider.de)
The 10 Scariest Things About Emergency Window Repair Window Repair
The 10 Most Scariest Things About Online Store Uk Cheapest online store Uk cheapest
https://images.google.com.sv/url?q=https://dzhenerik-dapoxetine-kupit.ru
Wonderful site you have here but I was curious about if you knew of any discussion boards that cover
the same topics discussed here? I’d really love to be a part of group where I can get comments from
other experienced people that share the same interest.
If you have any recommendations, please let me
know. Cheers!
Admiring the dedication you put into your site and in depth information you provide.
It’s great to come across a blog every once in a while
that isn’t the same outdated rehashed information. Wonderful read!
I’ve saved your site and I’m adding your RSS feeds to
my Google account.
What Window Glass Repair Near Me Could Be Your Next Big Obsession Window Repair
Upvc Door Lock Replacement: What’s No One Is
Talking About replacing lock on upvc door
pornoxer
What’s The Job Market For Car Accident Attorney Professionals
Like? car Accident law firm
Приветствую. Может кто знает, где почитатьразные статьи о недвижимости? Сейчас читаю https://universal37.ru
Double Glazing Cambridgeshire: What’s The Only Thing Nobody Is Discussing door Seal repair near me (calcal.net)
The Comprehensive Guide To Double.Glazing Near Me Double Glazing Windows
The Ultimate Cheat Sheet On Repair Double Glazed Windows
double glazed window repairs near me
WOW just what I was searching for. Came here by searching for BOWYANGS
Online Shopping Website In London’s History Of Online Shopping Website
In London In 10 Milestones Vimeo
A片
It’s genuinely very complex in this full of activity life to listen news on TV,
thus I only use web for that purpose, and take the latest news.
15 Gifts For The Repairs To Upvc Windows Lover In Your Life window Repairs
Great items from you, man. I have keep in mind your stuff prior to and you
are simply extremely great. I really like what you’ve received right here,
certainly like what you are saying and the way in which during which you are saying it.
You are making it entertaining and you still care for to stay it sensible.
I can’t wait to read far more from you. That is really a great website.
Corner Chaise Tools To Make Your Everyday Lifethe Only Corner Chaise Trick
That Should Be Used By Everyone Learn corner Chaise; vilhelmsen-ryan-2.technetbloggers.de,
tretinoin gel otc
10 Myths Your Boss Is Spreading Concerning Free Slots famous slots (https://Fushihara.hatenadiary.Jp)
10 Of The Top Facebook Pages Of All Time Best Slots hacksaw gaming slot machine game