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
Allow me reveal you in my testimonial of NewULife.
Here is my homepage – Bookmarks
Developers from all over the world pitch you ideas.
Feel free to visit my page t shirt design apps free
What is New U Life SOMADERM Transdermal Gel.
Feel free to visit my web-site :: nib foundation logo
In this instance, the life insurance policy company’s cash.
My blog post :: Nib Foundation Logo
Let me reveal you in my testimonial of NewULife.
Check out my webpage Bookmarks
In this case, the life insurance policy firm’s money.
My web-site … Bookmarks
Brandbusters TShirt Design Job Submission.
My page – Bookmarks
Профессиональный сервисный центр по ремонту принтеров в Москве.
Мы предлагаем: мастер по ремонту принтеров на дому
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Autolocksmith Near Me It’s Not As Hard As You Think mobile Locksmith
Hello there, just became aware of your blog through Google,
and found that it’s really informative. I am going to
watch out for brussels. I will be grateful if you continue this in future.
A lot of people will be benefited from your writing. Cheers!
In this instance, the life insurance policy company’s cash.
Feel free to visit my web site :: rems product list
What is New U Life SOMADERM Transdermal Gel.
my site: nib not working
NewULife Ranks. Welcome do you have to wear foundation with eye makeup my NewUlife Testimonial.
pharmacie en ligne: Medicaments en ligne livres en 24h – pharmacie en ligne livraison europe
Pharmacie Internationale en ligne: Medicaments en ligne livres en 24h – pharmacie en ligne pas cher
pharmacie en ligne livraison europe: pharmacie en ligne pas cher – pharmacie en ligne avec ordonnance
скуд комплект http://www.northern-computers.ru .
https://pharmaciepascher.pro/# pharmacie en ligne
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
network-1061063.mn.co/posts/65295432
Godfather789ยเว็บตรงของคนไทย เจ้าพ่อมาเฟียเว็บใหญ่ไม่มีโกง
http://clssansordonnance.icu/# Achat mГ©dicament en ligne fiable
Authentic HGH Advantages (NewULife Hgh Gel Not Checked Yet).
my web page … Bookmarks
A synthetic USP human development hormonal agent (somatropin).
Review my page … Bookmarks
In this instance, the life insurance policy business’s loan.
Also visit my web blog – foreign auto repair shop
instagram anonymous story viewer instagram anonymous story viewer .
NewULife Ranks. Invite to my NewUlife Testimonial.
My page :: vigabatrin rems survey
Сервисный центр предлагает срочный ремонт кофемашин morphy richards ремонт кофемашины morphy richards на дому
https://5stargamblingsites.com/
pharmacie en ligne france pas cher: Cialis generique achat en ligne – п»їpharmacie en ligne france
Chimineas For Sale Near Me Tools To Ease Your Daily Life Chimineas
For Sale Near Me Trick That Every Person Should Learn chimineas for
sale [Osvaldo]