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
I found this very helpful. For additional info, visit personal injury lawyer mobile al
Amazing! Its actually amazing article, I have got much clear idea about from
this article.
пинап казино: пин ап казино онлайн – пин ап казино
pinup kazi: пин ап казино – пин ап вход
вывод из запоя в стационаре воронежа aqvakr.forum24.ru/?1-7-0-00011578-000-0-0-1730749158 .
лечение наркозависимости в стационаре automobilist.forum24.ru/?1-19-0-00000140-000-0-0-1730749275 .
Do you have any video of that? I’d care to find out more details.
Полезная информация как купить диплом о высшем образовании без рисков
Five Killer Quora Answers To Repair Key Fob repair key fob
https://vavada-kazi.ru/# вавада казино
пин ап вход: pinup – pinup-kazi.ru
Диплом вуза купить официально с упрощенным обучением в Москве
Hey would you mind sharing which blog platform you’re
using? I’m planning to start my own blog soon but I’m having a difficult time deciding
between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique.
P.S Apologies for being off-topic but I had to ask!
пин ап зеркало: пин ап вход – пинап казино
Drugs information for patients. Effects of Drug Abuse.
order generic thorazine for sale
Some information about medicine. Read information now.
Asking questions are in fact pleasant thing if you
are not understanding something fully, but this article gives pleasant understanding
even.
how can i get calan without prescription
казино вавада: vavada – вавада казино
pinup пин ап казино пин ап казино
Any person checking out styles like wide-plank lumber floorings? They are actually undoubtedly creating waves this period! # # anyKey word # carpet near me
вывод из запоя в стационаре воронежа http://zavitai.mybb.social/viewtopic.php?id=91 .
Truly a good deal of great knowledge!
Feel free to surf to my web site – https://creditfreethai.com/casino-online/
Легальная покупка диплома о среднем образовании в Москве и регионах
Whiskey Bar 광주오피 (https://ilovebam-juso.Com/)
pinup-kazi.kz: pin up казино – пин ап кз
Exceptional prices allow you to tamoxifen and fatigue only after you have looked at competitive online specials
Paragraph writing is also a excitement, if you be familiar with after that you can write if not it is complicated to write.
Диплом пту купить официально с упрощенным обучением в Москве
выведение из запоя воронеж стационар https://www.setter.borda.ru/?1-7-0-00000675-000-0-0-1730749534 .
I every time used to read paragraph in news
papers but now as I am a user of internet therefore from now I am using net
for posts, thanks to web.