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
Founded in 2021, Jeetbuzz has rapidly grown in popularity across Bangladesh and Southeast Asia. Licensed by the government of Curacao, Jeetbuzz offers sports betting, casino games, and a betting exchange jeetbuzz bet
вывод из запоя вывод из запоя .
клиника вывод из запоя ростов клиника вывод из запоя ростов .
вывод из запоя круглосуточно ростов https://www.vyvod-iz-zapoya-rostov27.ru .
This was highly useful. For more, visit asphalt contractor
stamp maker online free stamp maker online free .
The salad bar at a Brazilian steakhouse is equally as impressive as the main course! So many fresh options to select from. Discover more concerning my preferred areas at passador brazilian steakhouse menu
вывести из запоя вывести из запоя .
Happy Hour Budal
The warmth could be insufferable with out a purposeful AC! I stumbled on the great answers at https://wiki-canyon.win/index.php/Experienced_HVAC_Contractors_in_Topanga_CA ; distinctly advised for residents of Canoga
https://storage.googleapis.com/digi216sa/research/digi216sa-(426).html
(I don’t think he will enjoy the journey of buying with me).
Ten Best Brand For Refrigerators That Really Change Your Life best brand
for refrigerators; Juliann,
It’s hard to find honest repair services these days, but iphone repair definitely fits the bill
One instance for CNS targeting using systemic distribution after IN application is the
management of anesthetics.
алкоголизм лечение вывод из запоя ростов алкоголизм лечение вывод из запоя ростов .
yat?r?ms?z deneme bonusu veren siteler: yat?r?ms?z deneme bonusu veren siteler – deneme bonusu veren yeni siteler
en kazancl? slot oyunlar? slot oyunlar? puf noktalar? slot oyunlar?
слотозал казино
Greate post. Keep posting such kind of info on your page.
Im really impressed by it.
Hey there, You have done an incredible job.
I’ll definitely digg it and personally recommend to my friends.
I’m confident they’ll be benefited from this web site.
sweet bonanza sweet bonanza oyna sweet bonanza
Heya i am for the primary time here. I came across this board and I to find It truly helpful & it helped me
out a lot. I’m hoping to offer something again and aid others like you
helped me.
Виртуальный номер телефона даёт возможность подключать любое количество номеров и разговаривать с любого устройства, принимать звонки в офисе или дома онлайн номер Казахстана
It’s awesome to visit this site and reading the views of all friends on the topic of
this post, while I am also keen of getting knowledge.
https://casinositeleri25.com/# guvenilir casino siteleri
Unquestionable Evidence That You Need Address Collection 주소모음사이트 (Gladis)
вывод. из. запоя. ростов. на. дону. вывод. из. запоя. ростов. на. дону. .
deneme bonusu veren siteler yeni denemebonusuverensiteler25 deneme bonusu veren siteler yeni
yeni deneme bonusu veren siteler yat?r?ms?z deneme bonusu veren siteler deneme bonusu veren siteler
en cok kazand?ran slot oyunlar? slot oyunlar? puf noktalar? slot casino siteleri
slot oyunlar?: slot oyunlar? – en kazancl? slot oyunlar?