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
How To Outsmart Your Boss In Tree House Bunkbeds High sleeper treehouse bed (http://wx.Abcvote.cn/)
http://ozempic.art/# buy ozempic
5 Killer Quora Answers On Integral Fridge Freezer integral fridge freezer (Karol)
В магазине сейфов предлагают купить сейф оптом заказать сейф
3 Ways In Which The Upvc Window Repairs Near Me Can Influence Your Life upvc windows repair near me
https://ozempic.art/# ozempic cost
В магазине сейфов предлагают сейфы купить в москве купить сейф интернете
Very shortly this web page will be famous among all blog users, due to it’s pleasant articles or reviews
The Worst Advice We’ve Seen About Car Boot Mobility Scooter Car Boot Mobility Scooter lightweight Collapsible mobility scooters
В магазине сейфов предлагают купить сейф оптом сейф цена купить
15 Things You Don’t Know About SEO London Seo London Uk
How To Stay Cutting Edge In Enterprise 에볼루션 코리아바카라 조작
Expert Advice On How To Check The Authenticity Of Pragmatic From A Five-Year-Old 프라그마틱 카지노
buy semaglutide pills rybelsus price rybelsus coupon
rybelsus price cheapest rybelsus pills semaglutide online
Hi there! This blog post could not be written much better!
Going through this article reminds me of my previous roommate!
He always kept preaching about this. I will forward
this information to him. Pretty sure he will have a great read.
Many thanks for sharing!
https://ozempic.art/# ozempic coupon
10 Things We All Love About Electric Wall.Mounted Fire best wall mounted electric fires (Brad)
buy ozempic pills online: ozempic coupon – ozempic coupon
срочный вывод из запоя на дому ростов срочный вывод из запоя на дому ростов .
Boot Scooter: The Good, The Bad, And The Ugly Mobility scooter portable car boot folding lightweight
semaglutide cost buy semaglutide pills rybelsus coupon
нарколог вывод из запоя ростов нарколог вывод из запоя ростов .
I read this post fully on the topic of the comparison of most up-to-date and earlier technologies, it’s remarkable article.
ozempic: ozempic online – buy cheap ozempic
Guide To Buy Electric Mobility Scooters: The Intermediate Guide Towards Buy Electric Mobility Scooters Buy electric mobility scooters
Hello to every single one, it’s actually a fastidious for me to pay a quick visit this website, it includes valuable Information.
Just desire to say your article is as surprising. The clarity in your post is simply
spectacular and i could assume you’re an expert on this subject.
Well with your permission allow me to grab your feed to keep up to date with forthcoming post.
Thanks a million and please continue the gratifying work.
ozempic generic ozempic generic ozempic online
Why Everyone Is Talking About Wall Mount Fireplace Today fireplace insert (Tamela)