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
buy viagra online
ultrabet bonus ultrabet ultrabet
you’re in point of fact a excellent webmaster. The website loading pace is amazing.
It seems that you’re doing any distinctive trick.
Also, The contents are masterpiece. you’ve done a
excellent activity on this matter!
Hi, I read your blogs like every week. Your humoristic style is witty, keep it up!
ultrabet bonus: ultrabet – ultrabet giris
Family members of Asbestos Legal workers too are
at risk. Mesothelioma can take between 30 and 60 years to develop following
exposure. Most cases appear in the pleura surrounding the lungs, whereas other
forms of the disease are found in the peritoneum.
The Most Common Mistakes People Do With Realistic Sex Dolls most realistic feeling sex
doll (Selena)
Great blog here! Also your site loads up very fast!
What web host are you using? Can I get your affiliate link to your host?
I wish my website loaded up as quickly as yours lol
casino siteleri win guvenilir casino siteleri Casino Siteleri
slot tr online: slot tr online – slot oyunlar? puf noktalar?
The Top Reasons Why People Succeed Within The Male Masturbation Sex Toys Industry Masturbation Machine
Тут можно преобрести купить сейф несгораемый противопожарный сейф
What Experts In The Field Want You To Know?
fiat key replacement
can i buy generic glycomet pills
I aⅼl tthe time emaiⅼed this wweb site post page to all my friends,because
if like to reаd іt after that my lіnkks will too.
Μy blog post porn
Five Sports Toto Lessons Learned From Professionals 스포츠토토
Официальное получение диплома техникума с упрощенным обучением в Москве
Легальная покупка школьного аттестата с упрощенной программой обучения
Купить диплом о среднем образовании в Москве и любом другом городе
Легальная покупка школьного аттестата с упрощенной программой обучения
Тут можно преобрести оружейный шкаф купить оружейные шкафы москва
Тут можно преобрести огнестойкий сейф купить огнеупорные сейфы
ultrabet tr online: ultrabet yeni giris 1125 – ultrabet tr online
I do agree with all of the ideas you’ve presented for your post.
They’re really convincing and can definitely work.
Nonetheless, the posts are very short for newbies.
Could you please prolong them a bit from next time? Thank you for the post.
Аттестат школы купить официально с упрощенным обучением в Москве
I could not refrain from commenting. Perfectly written!
Wow! In the end I got a website from where I be capable of really take useful facts regarding my
study and knowledge.
Ни одно застолье невозможно представить себе без освежающих безалкогольных напитков. Сейчас вам не придется тратить свое время и силы на то, чтобы ездить в магазины и покупать соки, минеральную воду доставка алкоголя на дом москва 24алкосервис
Guide To Treadmills That Fold Flat: The Intermediate Guide
The Steps To Treadmills That Fold Flat treadmills that Fold flat
Как официально купить диплом вуза с упрощенным обучением в Москве