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
купить диплом в киселевске prema-diploms.ru .
top online pharmacy india: Indian pharmacy international shipping – top 10 online pharmacy in india
купить диплом в улан удэ prema-diploms.ru .
male ed http://indianpharm24.pro/# buy prescription drugs from india
ed treatment pills https://mexicanpharm24.cheap/# pharmacies in mexico that ship to usa
The Secret Secrets Of Cut Car Key laser key Cutting
Реально ли приобрести диплом стоматолога? Основные шаги
purple pharmacy mexico price list mexican drugs purple pharmacy mexico price list
http://mexicanpharm24.cheap/# pharmacies in mexico that ship to usa
best natural cure for ed
Hi, i think that i saw you visited my site thus i came to “return the favor”.I’m attempting to
find things to enhance my web site!I suppose its ok to use a few of
your ideas!!
https://indianpharm24.pro/# cheapest online pharmacy india
how to get prescription drugs without doctor
ed drug comparison http://mexicanpharm24.cheap/# mexican pharmaceuticals online
купить диплом охраны prema-diploms.ru .
http://mexicanpharm24.cheap/# buying prescription drugs in mexico
ed vacuum pumps
Сколько стоит диплом высшего и среднего образования и как это происходит?
9 Things Your Parents Taught You About Ethanol Fireplaces fireplaces
What’s The Current Job Market For Lightweight Folding Rollator Walker With Seat Professionals?
lightweight folding rollator walker with seat, Adolph,
Hey! This post could not be written any better!
Reading this post reminds me of my good old room mate! He always kept talking about
this. I will forward this article to him. Pretty sure he will
have a good read. Thanks for sharing!
Medication information leaflet. Short-Term Effects.
cost of generic proscar
All trends of medicines. Read information now.
Полезная информация как купить диплом о высшем образовании без рисков
non prescription ed drugs http://mexicanpharm24.cheap/# best online pharmacies in mexico
купить диплом тгу prema-diploms.ru .
buy medicines online in india Pharmacies in India that ship to USA india pharmacy mail order
http://indianpharm24.pro/# indian pharmacy
viagra without a doctor prescription
mexican drugstore online Legit online Mexican pharmacy mexico drug stores pharmacies
It Is The History Of Togel Sydney situs togel online (Markus)
Hello! This is my first visit to your blog! We are
a team of volunteers and starting a new initiative in a
community in the same niche. Your blog provided us valuable information to work on. You have done a outstanding job!
ed treatment pills low cost pharmacy tadalafil without a doctor’s prescription
I am regular reader, how are you everybody? This
post posted at this site is truly fastidious.
https://w6.datakorea.org/
http://mexicanpharm24.cheap/# best online pharmacies in mexico
pharmacy drugs