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
Wow, awesome blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your website
is wonderful, as well as the content!
The 10 Most Terrifying Things About L Shaped Sectional With Chaise Sectional With
Chaise (Perfectworld.Wiki)
календарь онлайн
slot oyunlar? puf noktalar?: en kazancl? slot oyunlar? – slot oyunlar?
slot siteleri: slot siteleri – slot oyunlar?
matadorbet bid matadorbet matadorbet giris
Don’t buy from sites that say their stromectol and herbs at superb savings to help minimize symptoms and feel healthier
Five Killer Quora Answers On Best Accident Attorneys Accident attorney
15 Terms That Everyone Is In The Mesothelioma Lawsuits Industry Should Know mesothelioma attorney
deneme bonusu veren siteler 2024 https://slot-tr.online/# az parayla cok kazandiran slot oyunlar?
ultrabet giris: ultrabet bonus – ultrabet yeni giris 1125
where to buy cheap endep without rx
I visited multiple web sites however the audio feature for audio
songs current at this web page is really wonderful.
Hi to all, it’s truly a pleasant for me to visit this website, it consists
of helpful Information.
You’ll Never Guess This Automatically Folding Mobility Scooter’s Tricks automatically Folding Mobility scooter
deneme bonusu veren siteler https://ultrabet-tr.online/# ultrabet bonus
matadorbet matadorbet.bid matadorbet
What’s Next In Auto Injury Lawyers car accident lawyers
near me; https://jisuzm.tv,
коломна купить диплом many-diplom77.ru .
Why people still make use of to read news papers
when in this technological globe all is presented
on net?
An impressive share! I’ve just forwarded this onto a coworker who
has been conducting a little research on this.
And he in fact bought me dinner because I found it for him…
lol. So allow me to reword this…. Thanks for the meal!! But yeah,
thanx for spending some time to talk about this subject here
on your web site.
https://denemebonusuverensiteler.top/# deneme bonusu veren siteler denemebonusu2026.com
deneme bonusu veren siteler
matadorbet giris matadorbet giris matadorbet bid
https://slot-tr.online/# az parayla cok kazandiran slot oyunlar?
deneme bonusu veren siteler mycbet.com
14 Misconceptions Common To Sofas Sale big couches for sale;
eroticguide.tokyo,
The Leading Reasons Why People Achieve In The Best Mesothelioma Attorney Industry Mesothelioma lawsuit (https://articlescad.com/)
I got this site from my pal who told me on the topic of this web page
and now this time I am browsing this site and reading very informative articles at this place.
купить диплом 2000 1russa-diploms.ru .
Тут можно преобрести сейф огнеупорный сейф огнестойкий цена
Тут можно преобрести сейф под ружье оружейные сейфы купить