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://mexicanpharm24.cheap/# mexican pharmaceuticals online
treat ed
Аттестат 11 класса купить официально с упрощенным обучением в Москве
impotence treatment http://indianpharm24.pro/# mail order pharmacy india
10 Websites To Help You Learn To Be An Expert In ADHD Diagnosis
UK Private private Adhd Assessments
Hi there! I’m at work surfing around your blog from my new iphone
3gs! Just wanted to say I love reading your blog and look forward to all your posts!
Carry on the outstanding work!
Hmm is anyone else encountering problems with the pictures on this blog loading?
I’m trying to find out if its a problem on my end or if it’s the blog.
Any responses would be greatly appreciated.
I was curious if you ever considered changing the structure of your site?
Its very well written; I love what youve got to say. But maybe you could
a little more in the way of content so people could connect
with it better. Youve got an awful lot of text for only having 1 or 2 images.
Maybe you could space it out better?
ed drugs compared http://mexicanpharm24.cheap/# pharmacies in mexico that ship to usa
Your content is on fleek, loving every word!
buy prescription drugs from india: Order medicine from India to USA – pharmacy website india
Pushchair 2 In 1 Tools To Ease Your Everyday Lifethe Only Pushchair 2 In 1 Technique Every
Person Needs To Know pushchair 2 in 1 (Boris)
http://mexicanpharm24.cheap/# pharmacies in mexico that ship to usa
cat antibiotics without pet prescription
ed medicine http://indianpharm24.pro/# world pharmacy india
See What Bio Fuel Fireplace Tricks The Celebs Are Utilizing
Bio fuel fire [willysforsale.com]
buying from online mexican pharmacy mexico pharmacy mexican mail order pharmacies
Renault Key Replacement Near Me Tips To Relax Your Everyday Lifethe Only
Renault Key Replacement Near Me Trick That Every Person Should Learn Renault Key Replacement Near Me
buy prescription drugs from canada https://mexicanpharm24.cheap/# medication from mexico pharmacy
A comet is product of a number of materials, together with dust and plenty of different gasses.
best online pharmacies in mexico: Mexican pharmacy ship US – medicine in mexico pharmacies
legal to buy prescription drugs without prescription https://pharm24.pro/# best ed pills
buy prescription drugs without doctor: cheap prescription drugs – new ed drugs
Medicine information leaflet. Generic Name.
can i order tamoxifen pills
Everything about drugs. Read information now.
Где и как купить диплом о высшем образовании без лишних рисков
15 Gifts For The Sofas For Sale Near Me Lover In Your Life sectional couches for sale near me (Alexander)
cost generic gabapentin without insurance
medication from mexico pharmacy mexico pharmacy reputable mexican pharmacies online
new treatments for ed cheap drugs supplements for ed
http://pharm24.pro/# ed meds online without doctor prescription
pharmacy drugs
Thank you for any other informative web site. Where else may just I
get that kind of information written in such a perfect manner?
I have a undertaking that I’m simply now running on,
and I have been at the glance out for such information.
I got this web page from my buddy who informed me regarding this site and now
this time I am browsing this site and reading very informative articles or reviews at this place.