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
buy amoxicillin 500mg canada http://amoxilcompharm.com/# amoxicillin cephalexin
Сколько стоит диплом высшего и среднего образования и как его получить?
fast shipping prednisone: raypharm – cost of prednisone 40 mg
Полезные советы по покупке диплома о высшем образовании без риска
I always emailed this weblog post page to all my associates,
for the reason that if like to read it then my friends
will too.
Medicines information for patients. Generic Name.
can you get cheap trileptal without a prescription
Everything what you want to know about drugs. Get here.
Kadang link yang keliatannya aman ternyata malah phishing.
Selalu teliti URL-nya, pastikan protokol HTTPS aktif, dan hindari situs mencurigakan ya!.
buy 10 mg prednisone: cheap prednisone – prednisone 40 mg price
Many companies involved in the manufacturing of asbestos litigation (Aleida) knew about the dangers
however they did not inform their employees or their customers.
These negligent companies should be accountable for their actions.
You really make it seem so easy with your presentation but I find this
matter to be actually something which I think I would never understand.
It seems too complicated and extremely broad for
me. I am looking forward for your next post, I’ll try to get the hang
of it!
Легальная покупка диплома ПТУ с сокращенной программой обучения
prednisone tablets india: prednisone – prednisone pharmacy
can i buy cheap clomid no prescription: rexpharm – cost of cheap clomid tablets
medicine prednisone 5mg: prednisone – prednisone for sale without a prescription
Wizardshop.cc. Welcome to wizard shop cc for hight quality cvv.
Wizardshop.cc gives you quality cc – Use this
page to login or register!
generic prednisone pills prednisone 60 mg tablet medicine prednisone 10mg
Как правильно купить диплом колледжа и пту в России, подводные камни
I like the helpful information you provide in your articles.
I’ll bookmark your blog and check again here regularly.
I am quite certain I will learn plenty of new stuff right here!
Best of luck for the next!
amoxicillin 500mg https://amoxilcompharm.com/# purchase amoxicillin online without prescription
max pharm Priligy tablets priligy maxpharm
Как официально купить аттестат 11 класса с упрощенным обучением в Москве
amoxicillin 500mg pill http://prednisoneraypharm.com/# prednisone 20
Официальное получение диплома техникума с упрощенным обучением в Москве
Всё, что нужно знать о покупке аттестата о среднем образовании
Drugs information. Long-Term Effects.
cost of verapamil for sale
Some what you want to know about medicines. Get here.
Hello, everything is going fine here and ofcourse every
one is sharing facts, that’s in fact good, keep up writing.
Good day! This is my first visit to your blog! We are
a group of volunteers and starting a new initiative in a community in the same
niche. Your blog provided us useful information to work on. You have done a
extraordinary job!
prednisone 54899 order Prednisone 200 mg prednisone daily
prednisone for sale without a prescription: prednisoneraypharm – purchase prednisone canada
Valuable information. Fortunate me I found your web site unintentionally, and I’m shocked why this accident did
not took place in advance! I bookmarked it.