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! This is my 1st comment here so I just wanted to give a quick shout out and tell you I
genuinely enjoy reading your articles. Can you recommend any
other blogs/websites/forums that deal with the same subjects?
Thanks!
пин ап казино: пин ап кз – пин ап кз
вывод из запоя на дому https://superstar.ukrbb.net/viewtopic.php?f=3&t=684 .
pinup kazi pinup пин ап казино
бухгалтерия проф цена 1s-buhgalteriya-kupit.ru .
казино вавада вавада онлайн казино vavada kazi
This is very attention-grabbing, You are an excessively skilled blogger.
I’ve joined your rss feed and stay up for in search of more of your excellent post.
Also, I have shared your web site in my social networks
вывод из запоя наркологом вывод из запоя наркологом .
пин ап казино официальный сайт: pinup – пин ап зеркало
Hi, i think that i saw you visited my website thus i came to “return the favor”.I am trying to find things to enhance my site!I suppose its ok to use a few of your ideas!!
Wow that was unusual. I just wrote an really long comment but after I
clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over
again. Anyhow, just wanted to say wonderful blog!
вавада казино онлайн: vavada kazi – вавада
payday loan
can you get generic valtrex prices
пинап казино: pinup – pinup
пин ап казино официальный сайт: пин ап вход – pinup
I think the admin of this website is truly working
hard in support of his website, as here every material
is quality based data.
Mostbet-da o‘yin o‘ynash imkoniyati har doim mavjud | Mostbet mobil ilovasi bilan istalgan joyda o‘yinlardan bahramand bo‘ling | Mostbet onlayn kazinosi bilan o‘yin va g‘alaba yo‘lida keting | Mostbet bilan yuqori koeffitsiyentlar va katta yutuqlar oling | Mostbet-da yangi o‘yinlarni sinab ko‘ring va katta yutuqlar qo‘lga kiriting mostbet o’yinlari
Happy Hour 광주유흥
http://pinup-kazi.ru/# пин ап казино официальный сайт
пин ап казино: пин ап казино – pinup-kazi.kz
https://pinup-kazi.ru/# pinup
Mostbet mobil ilovasini yuklab oling va o‘yinni boshlang | Mostbet yangi foydalanuvchilar uchun bonuslar taklif etadi | Mostbet orqali har kuni yangi aksiyalar va bonuslar | Mostbet-da o‘yin va pul tikish imkoniyatlaridan foydalaning | Mostbet-da yangi o‘yinlarni sinab ko‘ring va katta yutuqlar qo‘lga kiriting mostbetazcasino.com.az
Вопросы и ответы: можно ли быстро купить диплом старого образца?
казино вавада вавада казино зеркало казино вавада
excellent post, very informative. I ponder why the opposite experts of this sector do not understand this.
You should continue your writing. I’m sure, you have a huge readers’ base already!
vavada: вавада казино онлайн – вавада казино зеркало
I am actually grateful to the owner of this site who has shared this great paragraph at
at this place.
Как приобрести диплом о среднем образовании в Москве и других городах