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
What Is Wordai Versus Article Rewriter? History Of Wordai Versus
Article Rewriter What is wordai
buy cytotec over the counter buy cytotec buy cytotec in usa
Who Is Responsible For A Michael Kors Designer Bags Budget?
12 Top Notch Ways To Spend Your Money michael Kors bags tote
generic propecia prices get propecia pills propecia online
order generic propecia no prescription: buy generic propecia pill – propecia generics
Just desire to say your article is as astounding. The clarity
in your post is simply spectacular and i could assume you are an expert on this subject.
Well with your permission allow me to grab your feed to keep updated with forthcoming post.
Thanks a million and please keep up the enjoyable work.
cost of cheap propecia tablets: cost of propecia without prescription – buy propecia pills
Useful information. Luccky mee I found your site unintentionally, and I am stunnned why his accident did not came
about earlier! I bookmarked it.
Review my web blog … monter kadłubów okrętowych norwegia
cipro 500mg best prices cipro online no prescription in the usa cipro pharmacy
The mind brain injury attorney hawaii lawyers at DE CARO & KAPLEN,
LLP.
each time i used to read smaller content which as
well clear their motive, and that is also happening with this post which I am reading at
this place.
does tamoxifen make you tired: does tamoxifen make you tired – tamoxifen side effects forum
buy cytotec online fast delivery buy cytotec buy cytotec over the counter
15 Best Pinterest Boards To Pin On All Time About CSGO Weapon Case esports 2014 Summer Case
buy cytotec online fast delivery order cytotec online buy cytotec pills
get generic propecia prices: buying propecia without insurance – order generic propecia without rx
cipro for sale where can i buy cipro online ciprofloxacin 500 mg tablet price
http://finasteride.store/# propecia cost
zestril over the counter: lisinopril 10mg price in india – buy lisinopril online
20 Things You Must Be Educated About Best Folding Treadmill
Small Space sale
I blog often and I seriously thank you for your content. This
great article has truly peaked my interest. I’m going to
book mark your blog and keep checking for new
details about once a week. I opted in for your RSS feed
too.
https://lisinopril.network/# lisinopril 25mg tablets
Unquestionably believe that which you said.
Your favorite justification appeared to be on the net the easiest thing to be aware of.
I say to you, I definitely get irked while
people consider worries that they plainly don’t know about.
You managed to hit the nail upon the top as well as defined
out the whole thing without having side effect , people can take
a signal. Will likely be back to get more. Thanks
https://cytotec.club/# order cytotec online
bookmarked!!, I love your blog!
cytotec abortion pill: buy cytotec online – buy cytotec pills
Nice post. I learn something totally new and challenging on websites I stumbleupon every day.
It’s always interesting to read articles from
other authors and use a little something from their websites.
buy cytotec over the counter: cytotec buy online usa – buy cytotec over the counter
http://finasteride.store/# cost of propecia
http://lisinopril.network/# lisinopril brand name australia