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 health and wellness of the dirt is a vital consider successful Farming. Practices like crop turning as well as composting can easily boost dirt quality and improvement plant turnouts.
They have items to offer or solutions to offer.
My site – most expensive wedding venues in texas
renova mexico pharmacy: kroger pharmacy gabapentin – best online ambien pharmacy
What’s up, I desire to subscribe for this blog to get hottest updates,
so where can i do it please help out.
levitra pharmacy prices: best online pharmacy no prescription viagra – 24 store pharmacy
Профессиональный сервисный центр по ремонту фото техники от зеркальных до цифровых фотоаппаратов.
Мы предлагаем: ремонт шторки затвора фотоаппарата
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Cialis Soft Flavored: rite aid pharmacy how many store – tacrolimus online pharmacy
I’d like to thank you for the efforts you have put in penning this
site. I’m hoping to see the same high-grade blog
posts from you later on as well. In fact, your
creative writing abilities has motivated me to get my own, personal site now 😉
It is comparable to filing for protection against financial
institutions.
Also visit my webpage: largest chapter 11 bankruptcies
AebgtydaY: finasteride india pharmacy – viagra from us pharmacy
loratadine pharmacy: domperidone uk pharmacy – seconal online pharmacy
Если вы искали где отремонтировать сломаную технику, обратите внимание – профи тех сервис челябинск
работа девушкам сочи эскорт
как правило в топ развлечений в в интернете https://secondstreet.ru/profile/StievenFry/ входит виртуальный-покер.
Are You Picking Up Women So? If Not, Here’s Why 부평오피
ретриты в россии https://ретриты.рф
ретрит центр сочи https://ретриты.рф
ретрит практика https://ретриты.рф
ретрит программа https://ретриты.рф
pharmacy class online: Super Avana – wellbutrin pharmacy online
ретрит перезагрузка https://ретриты.рф
I am genuinely thankful to the holder of this web page who has
shared this fantastic paragraph at at this time.
ретрит туры для мужчин https://ретриты.рф
The 12 Most Unpleasant Types Of Double Penetration Dildo People
You Follow On Twitter mature Double dildo (willysforsale.com)
ретрит медитация https://ретриты.рф
The 10 Most Terrifying Things About Faux Leather Couch Faux
Leather couch (125.141.133.9)
духовная практика ретрит https://ретриты.рф
Awesome article.
Heya i’m for the first time here. I came across this board and I in finding It truly helpful & it helped me out a lot.
I am hoping to provide something back and aid others such as you helped me.
target pharmacy flovent: online pharmacy loratadine – Dulcolax