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
ОНЛАЙН КАЗИНО
https://forum.zamki-kreposti.com.ua/profile/4846-comikini/
bitcoin dark web https://github.com/darkmarketlinkp22jr/darkmarketlink – dark market url
https://viagrameilleurprix.com/# Viagra pas cher inde
Achat mГ©dicament en ligne fiable
A synthetic USP human growth hormonal agent (somatropin).
Visit my web page :: ai detector free best
darknet drugs https://github.com/darknetmarketlistv8tg0/darknetmarketlist darknet markets onion
vente de mГ©dicament en ligne: pharmacie en ligne sans ordonnance – pharmacie en ligne sans ordonnance
darknet drug market https://github.com/darkmarkets2025we92r/darkmarkets2025 dark web markets
darknet drugs https://github.com/darknetdruglinksvojns/darknetdruglinks – darknet sites
What is New U Life SOMADERM Transdermal Gel.
my web blog – ppc management agency
In this instance, the life insurance business’s loan.
Here is my web-site google ads agency
In this case, the life insurance company’s loan.
Here is my page – ppc management agency
I quite like reading an article that can make men and women think.
Also, many thanks for allowing me to comment!
My web page; sexual assault lawyers
USP human development hormone (somatropin).
Also visit my site … lead generation agency
https://tadalafilmeilleurprix.shop/# pharmacie en ligne fiable
pharmacie en ligne avec ordonnance
darkmarkets https://github.com/darknetdruglinksvojns/darknetdruglinks – dark web link
NewULife Ranks. Invite to my NewUlife Review.
my site … ai detector quillbot
VA loans accept zero equity release calculator on a refinance.
dark market link https://github.com/darknetwebsitesgflpx/darknetwebsites – dark market url
I don’t even know the way I ended up right here, but
I thought this publish was once great. I don’t recognize who you might be but definitely you’re
going to a well-known blogger if you happen to are not already 😉 Cheers!
Here is my webpage :: sexual assault lawyers near me
The best CBD items are made from high-grade hemp cbd drops For cats.
Pharmacie sans ordonnance: pharmacie en ligne france – acheter mГ©dicament en ligne sans ordonnance
Real HGH Rewards (NewULife Hgh Gel Not Examined Yet).
My blog post … ai detector for teachers
They have products to market or services to provide.
Here is my blog post ai detector turnitin free online
What is New U Life SOMADERM Transdermal Gel.
my site … ai detector free grammarly
darknet markets 2025 https://github.com/darknetwebsitesgflpx/darknetwebsites – dark web market
Finding an accurate equity release vs secured loan release quote can be tough.
Hi there everybody, here every one is sharing these kinds of knowledge,
so it’s pleasant to read this web site, and I used to pay a quick
visit this web site daily.
Take a look at my webpage – sexual assault charges in texas
Precisely what I was searching for, appreciate it for putting
up.
Have a look at my web blog :: sexual assault lawyers in charlotte nc
NewULife Ranks. Invite to my NewUlife Testimonial.
Also visit my page – Ai Detector free
Land 3 bonus symbols to turn on the Jackpot Incentive Video Game.
My homepage :: online slots nz and online pokies nz