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
ultrabet tr online ultrabet giris ultrabet yeni giris 1125
Hiya very nice website!! Man .. Beautiful .. Superb ..
I will bookmark your website and take the feeds additionally?
I’m glad to seek out numerous helpful info here within the post,
we want work out more strategies on this regard, thanks for sharing.
. . . . .
9 Things Your Parents Taught You About French Door
Style Fridge Freezer french door style fridge (Ernest)
ultrabet: ultrabet – ultrabet guncel
https://slot-tr.online/# slot oyunlar? puf noktalar?
deneme bonusu veren yeni siteler
deneme bonusu veren yeni siteler deneme bonusu veren siteler betturkey deneme bonusu veren siteler betturkey
Процесс получения диплома стоматолога: реально ли это сделать быстро?
My brother suggested I may like this web site.
He was once totally right. This put up truly made my day.
You can not imagine simply how much time I had spent for this info!
Thanks!
10 Ways To Create Your Get An ADHD Diagnosis Empire how do you diagnose adhd in adults
Всё о покупке аттестата о среднем образовании: полезные советы
ou812chat.com/read-blog/4619
What’s The Reason You’re Failing At Upvc Window Handle Replacement upvc Bay window
denemebonusuverensiteler.top https://casinositeleri.win/# Casino Siteleri
кожзам цена кожзам цена .
Purchase medications at our online drugstore at stromectol tablets for your next medicine purchase.
сколько стоит оформить перепланировку квартиры сколько стоит оформить перепланировку квартиры .
denemebonusuverensiteler.top: deneme bonusu veren siteler denemebonusu2026.com – deneme bonusu veren yeni siteler
My partner and I stumbled over here by a different
website and thought I should check things out. I
like what I see so now i am following you. Look forward
to looking at your web page repeatedly.
slot oyunlar? puf noktalar?: az parayla cok kazandiran slot oyunlar? – slot tr online
anonymous viewing of instagram stories http://freeinstviewer.com .
Hello, yup this paragraph is actually good and I have learned
lot of things from it about blogging. thanks.
prednisone purchase online
deneme bonusu veren siteler betturkey https://ultrabet-tr.online/# ultrabet yeni giris 1125
5 ADHD Diagnosis UK Projects For Every Budget adhd How To Get diagnosed
Легальная покупка диплома о среднем образовании в Москве и регионах
octomo.co.uk/read-blog/3013
view instagram stories anonymously view instagram stories anonymously .
согласование перепланировки стоимость согласование перепланировки стоимость .
deneme bonusu veren siteler denemebonusu2026.com https://slot-tr.online/# slot tr online
Быстрая схема покупки диплома старого образца: что важно знать?
matadorbet giris matadorbet giris matadorbet bid
Как приобрести диплом техникума с минимальными рисками