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
10 Life Lessons We Can Learn From Slots kaymell.uk
центр ремонта стиральных машин http://www.centr-remonta-stiralnyh-mashin.ru .
Thank you for the good writeup. It in reality
used to be a amusement account it. Look complicated to more brought
agreeable from you! By the way, how could we keep up a correspondence?
I’m very pleased to discover this web site. I want to to thank you for your time
due to this wonderful read!! I definitely loved every little
bit of it and i also have you saved to fav to see new things on your
site.
Lorem Ipsum has been the industry’s standard dummy text https://onkopaednki.de/jkkqm of type and scrambled it to make a type specimen book.
Off-Page Seo For Organic Traffic 구글상위노출 업체
ремонт стиральных машин адреса ремонт стиральных машин адреса .
центр ремонта стиральных машин https://centr-remonta-stiralnyh-mashin.ru .
The Strategies For Availing A Home Loan 디딤돌 대출
Medicine information. Generic Name.
how to get cheap zanaflex without prescription
All trends of medicine. Read here.
сервис ремонта стиральных машин https://centr-remonta-stiralnyh-mashin.ru .
Ten Causes Of Mesothelioma Other Than Asbestoss That Really Make Your
Life Better cassylawn
мастерская по ремонту смартфонов мастерская по ремонту смартфонов .
сервис ремонта стиральных машин http://www.centr-remonta-stiralnyh-mashin.ru .
выезд мастера для ремонта стиральной машины http://www.centr-remonta-stiralnyh-mashin.ru/ .
Medicine information leaflet. Long-Term Effects.
get neurontin tablets
Best news about pills. Read here.
Hello, I enjoy reading through your article post. I like to
write a little comment to support you.
Приветствую. Может кто знает, где почитать полезные блоги о недвижимости? Сейчас читаю https://u-mechanik.ru
Приветствую. Подскажите, где найти разные блоги о недвижимости? Сейчас читаю https://u-mechanik.ru
Hi there this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or if you
have to manually code with HTML. I’m starting a blog soon but
have no coding expertise so I wanted to get advice from someone with
experience. Any help would be greatly appreciated!
Good day! I could have sworn I’ve been to this site before but after browsing through some of the post
I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be bookmarking and checking back often!
Are Search Male Or Female? 구글상위노출 트래픽
It is the best time to make some plans for the future and it’s time to be happy.
I’ve read this post and if I could I wish to suggest you few interesting things or suggestions.
Maybe you could write next articles referring to this article.
I wish to read even more things about it!
Thanks for finally writing about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Liked it!
Excellent blog here! Also your website loads up very fast!
What host are you using? Can I get your affiliate link to your host?
I wish my website loaded up as quickly as yours lol
ремонт стиральных машин дома на выезде https://centr-remonta-stiralnyh-mashin.ru .
How Much Interest Will I Pay On The Car Bank? 100만원 대출 (futureendeavorsinc.com)
I have been surfing on-line greater than 3 hours as of late, yet I
never discovered any fascinating article like yours.
It is beautiful value enough for me. In my view, if all web owners
and bloggers made excellent content material as you probably did, the internet will likely
be much more helpful than ever before.
Всем привет! Подскажите, где почитать полезные блоги о недвижимости? Пока нашел https://u-mechanik.ru
I need to to thank you for this excellent
read!! I absolutely enjoyed every bit of it. I have got you saved as a favorite
to look at new things you post…