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
15 Gifts For The Audi A4 Car Key Replacement Lover In Your
Life Audi Key Replacement Cost
What’s Holding Back This Fold Up Electric Wheelchair Industry?
compact folding electric Wheelchair
en kazancl? slot oyunlar?: az parayla cok kazandiran slot oyunlar? – en kazancl? slot oyunlar?
20 Resources That’ll Make You More Effective At ADHD Private Diagnosis private
adult adhd assessment belfast – Reginald,
slot tr online slot siteleri az parayla cok kazandiran slot oyunlar?
Тут можно преобрести сейфы оружейные сейф для охотничьего ружья купить
deneme bonusu veren siteler denemebonusu2026.com http://slot-tr.online/# slot oyunlar?
Good info. Lucky me I found your website by accident (stumbleupon).
I’ve saved as a favorite for later!
matadorbet giris: matadorbet bid – matadorbet bid
купить диплом в верхней пышме 1russa-diploms.ru .
The 12 Worst Types Of Users You Follow On Twitter
double Seat stroller
сервис накрутки подписчиков instagram сервис накрутки подписчиков instagram .
What’s The Job Market For Accident Claims Lawyers Professionals?
accident claims Lawyers (zenwriting.net)
deneme bonusu veren siteler denemebonusu2026.com https://denemebonusuverensiteler.top/# deneme bonusu veren siteler denemebonusu2026.com
Guide To Organic Coffee Beans 1kg: The Intermediate Guide On Organic
Coffee Beans 1kg organic coffee beans 1kg (Giselle)
Hi there, of course this paragraph is actually good and I have learned lot of things from it
about blogging. thanks.
Also visit my web blog; รับจัดงานแต่งงาน
купить диплом о образовании в москве цена купить диплом о образовании в москве цена .
Guide To Situs 4d: The Intermediate Guide To Situs 4d situs 4D
What’s up, I read your blogs like every week. Your writing style is awesome, keep up the good work!
ultrabet yeni giris 1125: ultrabet – ultrabet bonus
This piece of writing presents clear idea in support
of the new viewers of blogging, that in fact how to
do running a blog.
You could certainly see your enthusiasm in the article you write.
The arena hopes for even more passionate writers like you who are not afraid to mention how they believe.
At all times follow your heart.
casino siteleri win: guvenilir casino siteleri – Canl? Casino Siteleri
guvenilir casino siteleri: guvenilir casino siteleri – casino siteleri win
slot tr online en kazancl? slot oyunlar? slot oyunlar? puf noktalar?
What’s The Job Market For Distressed Leather Couch Professionals?
distressed leather couch (Andra)
Awesome! Its truly remarkable article, I have got much clear
idea concerning from this paragraph.
Five Killer Quora Answers On Automatic Folding Scooter Mobility automatic folding Scooter Mobility
You can definitely see your enthusiasm within the work you write.
The arena hopes for more passionate writers like you who are not afraid to
say how they believe. Always follow your heart.
https://paitosgpwarna.net/
Тут можно преобрести где купить сейф для оружия сейфы для оружия цены