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
Deneme Bonusu Veren Siteler guvenilir casino siteleri Canl? Casino Siteleri
You reported this very well!
Look at my blog :: https://QGB.Qualitygb.com/blog/index.php?entryid=181986
Официальная покупка диплома вуза с сокращенной программой в Москве
VIP Lounge 오피사이트
Goօd article! We will be linkіng to this great content on our
website. Keep up the great wгiting.
My web site dede4d
Can you tell us more about this? I’d love to find out some additional information.
en kazancl? slot oyunlar?: az parayla cok kazandiran slot oyunlar? – slot oyunlar? puf noktalar?
What’s The Current Job Market For Best Car Seat Newborn Professionals Like?
car Seat newborn
дермантин купить дермантин купить .
Тут можно преобрести сейф для оружие сейф сейфы оружейный оружие
купить кожзам для обивки мебели купить кожзам для обивки мебели .
Легальные способы покупки диплома о среднем полном образовании
view instagram stories anonymously view instagram stories anonymously .
проект перепланировки квартиры цена москва http://www.stoimost-soglasovaniya-pereplanirovki-kvartiry.ru .
Полезные советы по безопасной покупке диплома о высшем образовании
стоимость перепланировки квартиры стоимость перепланировки квартиры .
deneme bonusu veren siteler deneme bonusu veren siteler denemebonusu2026.com deneme bonusu veren siteler yerliarama.org
ultrabet bonus: ultrabet yeni giris 1125 – ultrabet giris
Официальное получение диплома техникума с упрощенным обучением в Москве
Hello, i think that i saw you visited my web site so i came
to “return the favor”.I’m attempting to find things to enhance my
website!I suppose its ok to use some of your ideas!!
Medication information. What side effects can this medication cause?
get zoloft without a prescription
Everything news about drugs. Read now.
What’s Taking place i’m new to this, I stumbled upon this I have
found It absolutely helpful and it has aided me
out loads. I am hoping to give a contribution & help different users like
its helped me. Great job.
deneme bonusu veren siteler yerliarama.org https://casinositeleri.win/# guvenilir casino siteleri
hello!,I like your writing so much! percentage we keep in touch more approximately
your article on AOL? I require a specialist on this house to
resolve my problem. May be that is you! Having a look forward to peer you.
Deneme Bonusu Veren Siteler Deneme Bonusu Veren Siteler Casino Siteleri
Casino Siteleri: Casino Siteleri – guvenilir casino siteleri
Aw, this was an incredibly nice post. Spending some
time and actual effort to generate a good article… but what
can I say… I put things off a whole lot and
never manage to get nearly anything done.
deneme bonusu veren siteler mycbet.com http://denemebonusuverensiteler.top/# deneme bonusu veren siteler 2024
deneme bonusu veren siteler https://slot-tr.online/# slot oyunlar?
Тут можно преобрести сейф под оружие оружейный шкаф сейф купить