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
The 10 Most Popular Pinterest Profiles To Keep Track Of Designer Handbags For Work
popular
camiseta españa 2022
buy kamagra online usa: kamagra.win – buy Kamagra
https://levitrav.store/# Buy generic Levitra online
An outstanding share! I have just forwarded this onto a co-worker who has been conducting a little research on this.
And he actually bought me breakfast simply because I found it for him…
lol. So allow me to reword this…. Thanks for the meal!!
But yeah, thanks for spending time to discuss this issue here
on your internet site.
My blog post :: 토토사이트순위
What Is Tommy Hilfiger Bookbag And Why Is Everyone Talking About It?
designer
http://kamagra.win/# Kamagra tablets
https://kamagra.win/# Kamagra Oral Jelly
What Experts From The Field Of Windows Repairs Want You To
Know? window Repairs
Cialis 20mg price in USA buy cialis online Buy Tadalafil 10mg
5 Common Myths About Work Jobs From Home You
Should Avoid work jobs from home
14 Businesses Are Doing A Fantastic Job At Double Glazing
In Leeds Repairs To Double Glazed Windows
How To Tell If You’re At The Right Level To Go After Local Window Repair window doctor [http://www.huenhue.net]
Sildenafil 100mg price: Buy Viagra online cheap – order viagra
Don’t Believe These “Trends” Concerning Vehicle Lock Repair Car security system Repair
Do Not Buy Into These “Trends” Concerning Audi Replacement Key replacement key Audi
I ⅼove what you gսys are usually up too. This kiind of clever work
and exposurе! Keep up the excellent works guys I’ve addsd you guys tо mʏ blogroll. https://Netcallvoip.com/wiki/index.php/Pagar_Beton_Lenggek_Bandung_Precast_Concrete
10 Quick Tips About Automotive Locksmiths Near Me automotive locksmiths near me; Rodrick,
cheapest cenforce: Cenforce 100mg tablets for sale – Cenforce 100mg tablets for sale
Cialis 20mg price in USA: Cialis 20mg price in USA – Cialis without a doctor prescription
See What Where To Buy Electronics Online Tricks The Celebs Are Making Use
Of where to buy electronics online [Julio]
This is nicely put. !
Five Lessons You Can Learn From Jobs Working Remotely work from home jobs bradford
“The Car Accident Attorney Awards: The Most, Worst, And Weirdest Things We’ve Seen car accident law firms
can i buy valtrex over the counter in australia
10 Facts About Upvc Window Repairs That Insists On Putting You In A
Good Mood upvc Window Repairs near me
https://viagras.online/# Generic Viagra online
you are truly a just right webmaster. The web site loading pace
is incredible. It sort of feels that you’re doing any
distinctive trick. Also, The contents are masterpiece.
you have done a excellent activity in this matter!
12 Facts About Double Glazing Windows Repairs To Make You Look Smart Around Other People replacing
7 Things You Didn’t Know About Window Repairs upvc Repairs