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
скуд купить комплект в москве http://www.northern-computers.ru .
They have items to offer or services to offer.
Feel free to visit my web blog :: replacement windows
in waco texas (Collin)
About Our Vehicle Mishap Lawyers Ratings.
Feel free to visit my web site … Bookmarks
60+ spectacular examples of geometric styles.
my web-site … Bookmarks
They have products to offer or solutions to use.
My web site Can a foundation Apply for grants
40 Examples of Bonus Cool TShirt Designs Lava360.
my web-site :: Bookmarks
Viagra pas cher livraison rapide france viagra sans ordonnance Viagra sans ordonnance 24h suisse
pharmacie en ligne france livraison internationale: Pharmacies en ligne certifiees – pharmacie en ligne fiable
Профессиональный сервисный центр по ремонту принтеров в Москве.
Мы предлагаем: ремонт принтера в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
15 Astonishing Facts About Sash Window Repair window Repairs
Pharmacie sans ordonnance: Medicaments en ligne livres en 24h – pharmacie en ligne sans ordonnance
What i don’t realize is in fact how you are no longer really a lot more neatly-liked than you may be right now.
You’re very intelligent. You realize thus significantly in the case of this subject, made
me personally imagine it from a lot of numerous angles. Its like women and
men aren’t fascinated unless it’s one thing to do with Woman gaga!
Your own stuffs nice. All the time maintain it up!
Achat mГ©dicament en ligne fiable: Cialis sans ordonnance 24h – Pharmacie Internationale en ligne
Genuine HGH Advantages (NewULife Hgh Gel Not Checked
Yet).
Here is my blog post … Bookmarks
USP human growth hormonal agent (somatropin).
my page; nib not working
What is New U Life SOMADERM Transdermal Gel.
Feel free to surf to my homepage: Nib Makeup Foundation
USP human development hormone (somatropin).
Here is my website: vigabatrin trade name
There are several locations to discover tee t shirt design app for ipad designs.
instagram stories anonymously instagram stories anonymously .
I’m not that much of a internet reader to be honest but your sites really nice, keep it up!
I’ll go ahead and bookmark your website to come back later.
Cheers
Feel free to visit my site; เครดิตฟรี38บาท
Обмены электронных валют мгновенны. Время перевода криптовалют зависит от скорости подтверждений сети, обычно занимает 5-30 минут после отправки. В случае загруженности сети блокчейн, данное время может увеличится paypal to usdt trc20
Developers from all over the world pitch you concepts.
Here is my web blog :: Bookmarks [Britt]
NewULife Ranks. Welcome to my NewUlife Review.
Take a look at my homepage – Bookmarks
acheter mГ©dicament en ligne sans ordonnance: Cialis prix en pharmacie – pharmacie en ligne avec ordonnance
Report the crash to your insurance company.
Also visit my web blog … truck accident attorney los angeles cz.law
In this case, the life insurance policy firm’s money.
Also visit my web-site – nib makeup foundation
USP human development hormonal agent (somatropin).
Also visit my homepage: Bookmarks
Upvc Window Repairs Near Me Tools To Streamline Your Everyday Lifethe Only Upvc
Window Repairs Near Me Trick Every Person Should Learn upvc window Repairs Near me
Сервисный центр предлагает качественый ремонт мфу ricoh мастер по ремонту мфу ricoh
Latest medication news. Medicine overview available.
buy valtrex uk online
Abuse effects detailed. Pill essentials explained.