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
Thanks for sharing your info. I really appreciate
your efforts and I am waiting for your further post thank you once again.
Всем привет! Подскажите, где найтиполезные статьи о недвижимости? Сейчас читаю – https://cspstroy.ru
Тут можно преобрести ящик для оружия купить магазин сейфы для оружия
prednisone 7.5 mg: Prednisone Without Prescription – prednisone buy canada
can you get clomid price: clomid purchase online rex pharm – where to buy generic clomid pills
amoxicillin generic: amoxil com pharm – amoxicillin 500mg capsules price
робокасса промокод https://www.promokod-robokassa.ru .
priligy max pharm: buy dapoxetine online – priligy maxpharm
Hi, i think that i saw you visited my site thus i came to “return the favor”.I’m trying
to find things to improve my website!I suppose its ok to use a few of your ideas!!
Всем привет! Подскажите, где почитатьразные блоги о недвижимости? Сейчас читаю – https://cspstroy.ru
Узнайте, как безопасно купить диплом о высшем образовании
I do agree with all the ideas you’ve presented to your post.
They are really convincing and can certainly work.
Nonetheless, the posts are too brief for beginners.
May just you please prolong them a little from next time?
Thank you for the post.
dark market link dark markets 2024 deep web search
priligy maxpharm: priligy – max pharm
Как получить диплом техникума с упрощенным обучением в Москве официально
Всем привет! Может кто знает, где почитатьполезные блоги о недвижимости? Пока нашел – https://cspstroy.ru
The 10 Most Terrifying Things About ADHD Diagnosis How to get diagnosed with adhd as an adult
Fantastic article Ill definitely be coming back to this for reference
sosro togel
order amoxicillin online: cheap amoxil – amoxicillin tablet 500mg
In recent years, http://w.dainelee.net/cgi-bin/pldbbs/pldbbs.cgi?p=1&ar=000434&comment=477&count=1&ie=1%5Dbuy products have gained immense popularity in the health and wellness industry. As more people seek natural alternatives to traditional medicine, CBD has emerged as a powerful plant-based remedy for various ailments.
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
priligy: max pharm – priligy max pharm
Drug information sheet. Cautions.
metformin hospitalized patients
Best trends of meds. Get information now.
Полезная информация как официально купить диплом о высшем образовании
I really appreciate your effort in helping people get the information they need 티비위키
buy cheap amoxicillin online: cheap amoxil – can i buy amoxicillin online
darkmarkets best darknet markets darknet market list
dapoxetine price: max pharm – max pharm
Как оказалось, купить диплом кандидата наук не так уж и сложно
Узнайте, как приобрести диплом о высшем образовании без рисков