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
mexican pharmaceuticals online mexican pharmaceuticals online mexican pharmaceuticals online
medicine in mexico pharmacies best mexican online pharmacies mexican pharmaceuticals online
https://mexicanph.com/# mexico drug stores pharmacies
purple pharmacy mexico price list
best online pharmacies in mexico mexican pharmacy mexican drugstore online
pharmacies in mexico that ship to usa mexican drugstore online mexican mail order pharmacies
mexican rx online mexican pharmaceuticals online mexican rx online
You actually said this terrifically.
pharmacy cheap no prescription online pharmacies in usa pharmacy prices compare
purple pharmacy mexico price list best mexican online pharmacies mexico pharmacies prescription drugs
Asking questions are really good thing if you are not understanding anything
fully, however this post provides fastidious understanding
yet.
https://radioluch.ru/club/?page=user_forum_message&user_id=13986&topic_id=6227&message_id=11186#message11186
buying prescription drugs in mexico online mexico pharmacies prescription drugs п»їbest mexican online pharmacies
purple pharmacy mexico price list buying prescription drugs in mexico medicine in mexico pharmacies
A motivating discussion is definitely worth comment. I do think that
you ought to write more on this issue, it might not be a taboo matter but usually
people do not discuss such subjects. To the
next! Many thanks!!
I’d like to thank you for the efforts you’ve put in writing this site.
I really hope to see the same high-grade content from you
later on as well. In fact, your creative writing abilities has motivated me
to get my own site now 😉
Hey great blog! Does running a blog similar to this require a lot of work?
I have very little expertise in coding but I had been hoping to start my own blog
soon. Anyway, if you have any recommendations or tips for new
blog owners please share. I know this is off subject nevertheless
I simply wanted to ask. Cheers!
раскрутка сайта продвижение сайта https://prodvizhenie-sajtov15.ru/ .
pharmacies in mexico that ship to usa mexican border pharmacies shipping to usa mexico drug stores pharmacies
Do you mind if I quote a few of your articles as long as I provide credit and sources back to your blog?
My blog is in the very same niche as yours and
my users would certainly benefit from some of the information you provide here.
Please let find sex near me know if this alright with
you. Regards!
сколько стоит аудит сайта prodvizhenie-sajtov15.ru .
I do not even know how I ended up here, but I thought this
post was great. I do not know who you are but definitely
you’re going to a famous blogger if you are not already 😉 Cheers!
My brother recommended I might like this web site. He used to be totally right.
This post truly made my day. You can not consider simply how so much time I had spent for
this information! Thank you!
mexican mail order pharmacies best online pharmacies in mexico mexico drug stores pharmacies
I absolutely love your blog.. Great colors & theme.
Did you build this site yourself? Please reply back as I’m attempting to create my own personal website and would love to learn where you got this from or just what the theme is named.
Kudos!
http://mexicanph.com/# mexican drugstore online
pharmacies in mexico that ship to usa
Howdy tһijs is kind of of ᧐ff topic but I was wondering if blogs use WYSIWYG
ditors oг if yoս һave to manually code with HTML.
I’m startijng a blog soon but have no copding experience so I
wanted to get guidance from someone with experience. Any һelp ԝouⅼd be enormously appreсiated!
medication from mexico pharmacy reputable mexican pharmacies online pharmacies in mexico that ship to usa
medicine in mexico pharmacies pharmacies in mexico that ship to usa mexican pharmaceuticals online
Interesting blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple tweeks
would really make my blog jump out. Please let me know where you got your design.
Appreciate it
Hello, just wanted to say, I loved this blog post.
It was inspiring. Keep on posting!
Мастер и Маргарита