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
Системно-семейные расстановки по Хеллингеру. https://rasstanovkiural.ru
guvenilir slot siteleri 2024: bonus veren casino slot siteleri – slot siteleri
https://slotsiteleri.bid/# casino slot siteleri
slot oyunlar? siteleri: slot siteleri guvenilir – canl? slot siteleri
It’s the best time to make a few plans for the long run and it is
time to be happy. I’ve learn this publish and if I may I desire to counsel you few attention-grabbing issues
or tips. Maybe you could write next articles relating to
this article. I wish to learn even more things about it!
my web blog :: USA Script Helpers
http://sweetbonanza.network/# sweet bonanza mostbet
My partner and I absolutely love your blog and find
a lot of your post’s to be exactly what I’m looking for.
can you offer guest writers to write content in your case?
I wouldn’t mind publishing a post or elaborating on a lot of
the subjects you write concerning here. Again, awesome web site!
When some one searches for his required thing, thus he/she wants to be available that in detail, therefore that thing is maintained over here.
Evden Eve Nakliyat | Taşınma sırasında doğru planlamanın önemini anlatan bu makale, müşterilerimize rehberlik edecek nitelikte. Kozcuoğlu Evden Eve Nakliyat olarak, taşınma sürecini her zaman en iyi şekilde yönetiyoruz.
Hi there mates, pleasant paragraph and fastidious arguments commented at this place, I am truly enjoying by these.
sapporo88
en guvenilir slot siteleri: slot casino siteleri – 2024 en iyi slot siteleri
Drugs information sheet. Generic Name.
where can i buy cheap compazine for sale
Some trends of medicine. Read information here.
slot kumar siteleri: deneme bonusu veren slot siteleri – slot siteleri 2024
Automaty do counter strike: global offensive – pokazy, dostępne promocje.
Badania demonstrują, że zachęca własnych graczy do ustalania przeciwwskazań
i paysafecard, https://experiment.com/users/Forpart z Polski.
gay porn
casino
Εverything is very open ѡith a precise description of the issues.
It was ɗefіnitely infⲟrmatіve. Your site is
useful. Thanks for sharing!
Here is my site :: 1
sapporo 88
11 Super Steps Giving The Perfect Massage Along With A Massage Stone 대덕구오피 (http://fitnessthroughdance.com/)
Why users still use to read news papers when in this technological globe all is accessible on web?
Hmm it appears like your website ate my first comment
(it was super long) so I guess I’ll just sum it up what I
had written and say, I’m thoroughly enjoying your blog.
I as well am an aspiring blog blogger but I’m still new to everything.
Do you have any points for inexperienced blog writers? I’d
really appreciate it.
2024 en iyi slot siteleri: en iyi slot siteleri 2024 – slot oyunlar? siteleri
This is a topic that is near to my heart… Thank you!
Exactly where are your contact details though?
my blog – pronerve 6 reviews
sapporo88
http://slotsiteleri.bid/# slot siteleri
http://denemebonusuverensiteler.win/# deneme bonusu
slot kumar siteleri: en iyi slot siteleri – casino slot siteleri
http://denemebonusuverensiteler.win/# deneme bonusu veren siteler
Lawinplay – You can Use GCash to play in anytime
Free Spins 1000+ Free Demo Slot
Openning Bonus 120% piso back
Searching lawinplay888 on Google or Bing to get free coins
https://lawinplay888.com