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
Hey There. I discovered your blog the use of msn. This
is a very smartly written article. I will be
sure to bookmark it and return to learn extra of your useful info.
Thank you for the post. I’ll certainly comeback.
https://casinositeleri25.com/# Canl? Casino Siteleri
This was beautifully organized. Discover more at hardwood floor refinishing near me
This was very enlightening. More at drug detox omaha
Helpful information, Cheers!
https://objectstorage.ap-tokyo-1.oraclecloud.com/n/nrswdvazxa8j/b/digi184sa/o/research/digi184sa-(441).html
Then you’ll view your saved listings each time you login.
вывод из запоя на дому ростов недорого вывод из запоя на дому ростов недорого .
вывод из запоя в ростове на дону вывод из запоя в ростове на дону .
слоты
en kazancl? slot oyunlar?: slot oyunlar? – en kazancl? slot oyunlar?
sweet bonanza demo oyna: sweet bonanza oyna – sweet bonanza kazanma saatleri
вывод из запоя круглосуточно ростов https://vyvod-iz-zapoya-rostov27.ru/ .
Você abordou tudo que eu precisava saber sobre a Riviera Francesa! Vou explorar mais conteúdo em Roteiros exclusivos Riviera Francesa
This is a topic that’s near to my heart… Best wishes!
Exactly where are your contact details though?
Night Club 오피
buy viagra online
This post presents clear idea designed for the new viewers of blogging, that genuinely how to do running a blog.
My blog auto phone case
slotozal kazino
вывести из запоя вывести из запоя .
tor markets links darknet site dark web drug marketplace
Pembahasan yang bagus! Untuk informasi tambahan, Anda bisa mengunjungi KADEKTOTO SLOT GACOR.
yat?r?ms?z deneme bonusu veren siteler: deneme bonusu veren siteler yeni – yat?r?ms?z deneme bonusu veren siteler
slotozal kazino
This was quite informative. For more, visit mortgage broker Memphis
This was quite informative. For more, visit roof repair near me
вывод из запоя в стационаре ростова https://vyvod-iz-zapoya-rostov27.ru/ .
Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.
Thanks a bunch for sharing this with all folks you actually realize what you’re
speaking approximately! Bookmarked. Kindly also seek advice from my site =).
We may have a link trade agreement between us
Убедительная просьба дополнительно уточнять указанные данные по электронной почте или контактным телефонам.
Thank you, I’ve just been searching for information approximately this subject for a
while and yours is the greatest I have discovered till now.
However, what about the bottom line? Are you certain about the
source?