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
Viagra Tablet price: FastPillsForMen – Sildenafil Citrate Tablets 100mg
I am truly thankful to the holder of this web site who has shared
this enormous article at at this time.
I found this very interesting. For more, visit برگرداندن پست حذف شده اینستاگرام
cheap ed drugs FastPillsEasy ed pills cheap
наркологическая скорая в москве наркологическая скорая в москве .
срочная наркологическая помощь в москве срочная наркологическая помощь в москве .
buy cialis pill buy cialis pill Generic Cialis price
http://fastpillsformen.com/# Sildenafil Citrate Tablets 100mg
Cheap Cialis: Max Pills For Men – cialis for sale
вызвать наркологическую помощь вызвать наркологическую помощь .
частная скорая наркологическая помощь частная скорая наркологическая помощь .
My brother suggested I might like this web site. He was entirely right.
This post truly made my day. You cann’t imagine just how much time I had spent for this info!
Thanks!
наркологическая скорая бесплатная наркологическая скорая бесплатная .
вызов нарколога на дом частная скорая помощь вызов нарколога на дом частная скорая помощь .
Swedish Massage OP
Hi to every one, for the reason that I am truly eager of reading this
web site’s post to be updated on a regular
basis. It includes good material.
Amazing piece! The way you handled this topic is remarkably innovative.
I’ve dedicated time considering this issue for some time, and your insights resonated with
me.
How you broke down the core elements was brilliant.
I specifically appreciated your analysis of the wider impact.
Looking forward to your next piece! Your perspective keeps delivering valuable insights.
Keep sharing such insightful pieces!
I’ll definitely pass this along with my network. This deserves broader exposure!
Much appreciated for this excellent article!
Check out my homepage :: Chicago Press (chicagomedia16.werite.net)
неотложная наркологическая помощь в москве неотложная наркологическая помощь в москве .
наркологическая скорая в москве наркологическая скорая в москве .
срочная наркологическая помощь в москве http://klin.0pk.me/viewtopic.php?id=4428 .
best price for viagra 100mg cheap viagra over the counter sildenafil
http://fastpillseasy.com/# online ed pharmacy
частная скорая наркологическая помощь частная скорая наркологическая помощь .
наркологическая скорая помощь москва наркологическая скорая помощь москва .
where can i get ed pills: FastPillsEasy – online erectile dysfunction prescription
срочное оформление загранпаспорта срочное оформление загранпаспорта .
The 1win promo code: MAX500WIN. This 1Win bonus code 2025 rewards new players with a 500% bonus up to $1025. Valid codes for receiving bonuses without a deposit for new and old users. Hurry to get them today! Activate the promo code 1win and get a generous welcome bonus of up to 500% on your first four deposits.
1win bonus code today no deposit
카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트
카지노사이트
You’ll Be Unable To Guess Best Accident Injury Lawyers’s Benefits best accident injury lawyers [https://yogicentral.science/wiki/13_things_about_Good_accident_attorney_you_may_not_have_Known]
I relish, result in I found just what I was having
a look for. You have ended my four day long hunt!
God Bless you man. Have a great day. Bye