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
фонбет фонбет .
Greetings I am so glad I found your blog, I really found you by error, while I was searching on Digg for something else, Nonetheless I am here now and would just like to say thanks a lot
for a fantastic post and a all round interesting blog (I also love the theme/design), I don’t have time to read through it
all at the minute but I have saved it and also added your RSS feeds, so when I have time I will be back to
read a lot more, Please do keep up the fantastic work.
amoxicillin 250 mg: cheap amoxil – amoxicillin 500 mg cost
Узнай все о операция по увеличению пениса цена операции по увеличению полового члена
Можно ли купить аттестат о среднем образовании, основные моменты и вопросы
Spot on with this write-up, I honestly believe that this site
needs far more attention. I’ll probably be returning to read
more, thanks for the info!
certainly like your web site however youu ave to test tthe
spelling onn quite a few of your posts. Many of them are rife with
spelling problems and I in finding it very troublesome to infrm the truth however I’ll certainly come again again. https://storage.googleapis.com/g7a/Wordpress-website-development/WORDPRESS-SUPPORT-&-MAINTENANCE/WordPress-Speed-And-Performance-Optimization-Service.html
вывод из запоя краснодар на дому http://www.mymoscow.forum24.ru/?1-6-0-00022914-000-0-0-1730745663 .
стекло для комацу
I know this web site presents quality based articles and additional stuff,
is there any other web page which gives these things in quality?
Generally I do not learn article on blogs, but I would like to say that this write-up very compelled me to take a look at
and do so! Your writing taste has been surprised me.
Thank you, quite nice article.
Легальные способы покупки диплома о среднем полном образовании
Узнай все о операция увеличение члена сколько стоит увеличение полового члена
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
Roulette Wheel Betting 다바오충전
Как оказалось, купить диплом кандидата наук не так уж и сложно
priligy: cheap priligy – dapoxetine online
Namun, sepak bola lebih populer hampijr diseluruh dunia,
tidak sepeti permainan bola basket yang terkenal hanya
dari di beberapa negara saja. Kamu bixa konsultasi dengan dokter jantung di Halodoc
yang tersedia 24 jam kapan saja dan di mana saja.
Heya i am for the first time here. I found this board and
I find It truly useful & it helped me out much. I hope to give something back and help others like
you helped me.
Thank you for sharing your info. I truly appreciate your efforts and I will be waiting for your next post thanks once again.
Have you ever attempted utilizing hot water in your pressure washer? Does it actually make that much difference? pressure washing
выведение из запоя воронеж стационар my.forum2.net/viewtopic.php?id=174 .
Hi, I read your blogs on a regular basis. Your humoristic style is witty, keep doing what you’re doing!
заказать алкоголь с доставкой москва круглосуточно
antiplatelet drug PlavixClo Best Price buy clopidogrel online
Узнай все о операция по увеличению члена цена удлинение полового члена цена
That is really fascinating, You’re an excessively skilled blogger.
I’ve joined your feed and look ahead to in quest of more of your excellent post.
Additionally, I have shared your web site in my social networks
Узнай все о увеличение размера полового члена увеличение полового члена хирургическим путем
Appreciate the recommendation. Let me try it out.
The aesthetic appeal of metal roofs can’t be denied—they add character to any home! Explore styles at metal roofers