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
Drugs information. What side effects can this medication cause?
azithromycin making me feel sick
Actual trends of pills. Read information now.
I constantly spent my half an hour to read this blog’s content every day along with a cup of
coffee.
Тут можно преобрести магазин сейфы для оружия сейф оружейный
The Unspoken Secrets Of Private ADHD Assessment Near Me private adhd assessment dundee (Tory)
deneme bonusu veren siteler betturkey betturkey.com https://casinositeleri.win/# casino siteleri win
It’s remarkable in support of me to have a
website, which is valuable in favor of my experience.
thanks admin
ultrabet tr online ultrabet giris ultrabet guncel
Where Are You Going To Find Folding Electric Wheelchairs For Sale
Be One Year From Today? Lightest electric Folding wheelchair
The 10 Most Scariest Things About Senior Mobility Scooters Premium mobility
Scooters (mysocialguides.Com)
deneme bonusu veren siteler denemebonusu2026.com: deneme bonusu veren siteler mycbet.com – deneme bonusu veren yeni siteler
Тут можно преобрести сейф для хранения оружия сейф для охотничьего ружья купить в москве
I don’t even understand how I stopped up here, however I assumed
this publish used to be great. I do not know who you’re however definitely you’re going
to a well-known blogger if you happen to are not already.
Cheers!
When some one searches for his vital thing, so he/she desires to
be available that in detail, so that thing is maintained over
here. https://Andyfreund.de/wiki/index.php?title=Comptoir_En_Granit_%C3%83_Mascouche_:_Caract%C3%83_ristiques_Et_Avantages
matadorbet giris: matadorbet.bid – matadorbet.bid
Можно ли купить аттестат о среднем образовании, основные моменты и вопросы
vv.flybb.ru/viewtopic.php?f=23&t=2537
Casino Siteleri Casino Siteleri casino siteleri win
Тут можно преобрести сейф для ружья цена сейф для оружия цены
Feel free to visit my web blog – Daycares popular listings (http://kwar.net)
ultrabet ultrabet ultrabet giris
ultrabet ultrabet ultrabet tr online
Hi there, You’ve done a fantastic job. I’ll definitely digg it and personally suggest to my friends.
I’m sure they’ll be benefited from this website.
Take a look at my site :: zapada01
ultrabet tr online: ultrabet giris – ultrabet
ultrabet ultrabet ultrabet bonus
anonymous instagram story viewer anonymous instagram story viewer .
You’ll Be Unable To Guess Self Propelled Wheelchairs For Sale’s
Benefits self Propelled wheelchairs
Five Killer Quora Answers On Cheap Bunk Beds
For Kids cheap bunk beds for kids [Leanna]
14 Cartoons About Audi Key Replacement That’ll Brighten Your Day
Car
az parayla cok kazandiran slot oyunlar?: slot oyunlar? – az parayla cok kazandiran slot oyunlar?
Тут можно преобрести сейф для сайги 12 сейф оружейный купить
ultrabet guncel: ultrabet guncel – ultrabet yeni giris 1125